I have a custom page where I try to change the page title.
The function executes, but the title is not changed. This is the code I’m using:

  add_filter('wp_title', set_page_title($brand));

  function set_page_title($brand) { 
    $title="Designer ".$brand['name'].' - '.get_bloginfo('name');
    //Here I can echo the result and see that it's actually triggered
    return $title;  
  } 

So why is this not working? Am I using add_filter wrong?

3 s
3

wp_title filter is deprecated since WordPress 4.4 (see here). You can use document_title_parts instead

function custom_title($title_parts) {
    $title_parts['title'] = "Page Title";
    return $title_parts;
}
add_filter( 'document_title_parts', 'custom_title' );

In custom_title filter, $title_parts contains keys ‘title’, ‘page’ (the pagination, if any), ‘tagline’ (the slogan you specified, only on homepage) and ‘site’. Set “title” the way you like. WordPress will keep your configured format to then concatenate everything together.

If you want to override WordPress title formating, then use pre_get_document_title and provide to that filter a function that takes a string and returns the title you want.

function custom_title($title) {
    return "Page Title";
}
add_filter( 'pre_get_document_title', 'custom_title' );

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *