How Do I Set the Page Title Dynamically?

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”.

Read More

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

Related posts

Leave a Reply

7 comments

  1. 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

  2. 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;
    }
    
  3. 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';
      }
    }
    
  4. 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

  5. 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;
    }
    
  6. 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.