27

Is it possible to change the page title with code?

For example, let's say the page's name is "Book your Order", but I want to change it to "Book Order #123".

I Google'd a bit and looked here and didn't see anything. Anyone know of a plugin or hack?

wp_title returns the page title but doesn't allow setting the page title: http://codex.wordpress.org/Function_Reference/wp_title

1
  • Where would the value come from? what has in that page the value of #123 ? Commented Nov 7, 2011 at 18:51

7 Answers 7

27

There is no documentation on it but you could always apply a filter to the_title like this:

add_filter('the_title','some_callback'); function some_callback($data){ global $post; // where $data would be string(#) "current title" // Example: // (you would want to change $post->ID to however you are getting the book order #, // but you can see how it works this way with global $post;) return 'Book Order #' . $post->ID; } 

See these:

http://codex.wordpress.org/Function_Reference/the_title

http://codex.wordpress.org/Function_Reference/add_filter

3
  • This seems to override all titles. How do I override only the current title? Commented Mar 29, 2017 at 16:47
  • You would need to add a condition to the callback, e.g if ($post->ID == 45) { ... } Commented Jul 9, 2018 at 3:55
  • 4
    the_title filter no longer works in the latest versions of Wordpress, use document_title_parts or pre_get_document_title filters as detailed in other answers. Commented Nov 15, 2018 at 2:48
17

As of Wordpress 4.4, you can use the Wordpress filter document_title_parts to change the title.

Add the following to functions.php:

add_filter('document_title_parts', 'my_custom_title'); function my_custom_title( $title ) { // $title is an array of title parts, including one called `title` $title['title'] = 'My new title'; if (is_singular('post')) { $title['title'] = 'Fresh Post: ' . $title['title']; } return $title; } 
2
  • but where do you pass in the parameter to a filter? Commented Dec 22, 2018 at 5:38
  • The above function modifies the way the_title() and get_the_title() functions work - so no need to pass any parameters. Commented Dec 25, 2018 at 19:51
8

For those wishing to change the document's title attribute, I found that using the wp_title filter no longer works. Instead, use the pre_get_document_title filter:

add_filter("pre_get_document_title", "my_callback"); function my_callback($old_title){ return "My Modified Title"; } 

Source

2
  • 1
    thanks for coming back years later to post this update. I had been using wp_title in a plugin of mine for years and hadn't realized it was no longer working until now and your answer saved me a lot of effort. So Thank you! Commented Jan 11, 2019 at 16:35
  • @MatthewLee Glad to hear it helped you :) Commented Jan 11, 2019 at 18:52
7

When having Yoast enabled you need to override the title like so:

add_filter('wpseo_title', 'custom_titles', 10, 1); function custom_titles() { global $wp; $current_slug = $wp->request; if ($current_slug == 'foobar') { return 'Foobar'; } } 
2

Really depends if you're looking to display a custom title for the current page (i.e. the contents of the <title></title> tag in the header) or filter the title of pages in the page body or in listings.

In the former case (the title of the current page), try adding a filter for wp_title() like so: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title

If you want to modify page titles across the board, filtering the_title() will do the trick: http://codex.wordpress.org/Plugin_API/Filter_Reference/the_title

4
  • Actually in my experience you need to filter both wp_title and the_title to cover both. Commented Mar 26, 2015 at 8:59
  • I am not sure if its because of the deprecation but tis doesnt work for me. I have tried combinations and inline filters and the new apply_filters( 'pre_get_document_title', string $title ) Commented Jan 18, 2017 at 18:00
  • sadly neither worked for me either. Commented Jun 8, 2019 at 5:46
  • This answer is nearly 6 years old; as the poster (and someone who doesn't actively work with WP anymore), I would suggest looking at the latest documentation instead. Commented Jun 9, 2019 at 19:33
1

If you're using All In One Seo v4+, use this filter:

add_filter( 'aioseo_title', 'aioseo_filter_title' ); function aioseo_filter_title( $title ) { if ( is_singular() ) { return $title . 'some additional title content here'; } return $title; } 
-1

Actually the easiest way to do this is use one line of js.

Put the following code in the template:

<script>document.title = "<?php echo $new_title; ?>";</script> 

This code doesn't has to be in the html header, it can be put in the html body.

2
  • 1
    This won't be good for SEO though. Commented Nov 21, 2020 at 0:57
  • 1
    @AlexCook You are definitely correct. I was working on a quick fix for something where SEO didn't matter. Now I totally forgot what this was used for. lol. Commented Nov 21, 2020 at 15:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.