Wordpress - How Do I Set the Page Title Dynamically?

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


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;
}

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

Tags:

Seo

Title