10

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?

1
  • If you only want to change the title for a specific template I believe it would be more suitable to call your custom title function in place of wp_title in the template markup. Dec 27, 2011 at 19:51

3 Answers 3

20

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' );
3
  • 2
    wp_title has been reinstated. By following your link above, you will find the comment: UPDATE 12 November [2017] – wp_title has been reinstated until alternative usages have been identified and a path forward for them defined.
    – amurrell
    Oct 26, 2018 at 23:20
  • 2
    As of July 2020 document_title_parts worked for me whereas wp_title did not. Jul 9, 2020 at 14:16
  • 1
    @GreatBlakes This is still the case on my end. As of March 2022, document_title_parts worked for me whereas wp_title did not. Mar 25, 2022 at 0:08
3

You cannot pass an array to set_page_title, the filters callback accept only the original title as input parameter.

If you want your function work this way, define the $brand array outside the function and make it global

  add_filter( 'wp_title', 'set_page_title' );
  $brand = array( 'name' => 'Brand Name' );

  function set_page_title( $orig_title ) { 
    global $brand;
    $title = 'Designer '.$brand['name'].' - '.get_bloginfo( 'name' );
    //Here I can echo the result and see that it's actually triggered
    return $title;  
  }
3
  • When I do this, the function is not called at all. I also tried add_filter('wp_title', 'set_page_title'); The only way to call the function is by doing this: add_filter('wp_title', set_page_title('test'));. Any reason why I'm not able to call the function your way?
    – Steven
    Feb 13, 2011 at 13:01
  • I think I know why. Could it be that I'm trying to use add_filter inside custom_template.php and not in my function.php ?
    – Steven
    Feb 13, 2011 at 15:39
  • You must place this function in your function.php file that is in your theme directory!
    – keatch
    Feb 13, 2011 at 16:22
0

What is happening in both examples of code is that add_filter is not calling a function to accept a return as the new title. The function add_filter expects at least two parameters, the first being a string that contains the 'tag' or name of the filter you would like to use, and the second is mixed. In the case of using a defined function you should use the function name as a string, it can also accept an array for a callback within a namespace, and an anonymous function.

In the case of:

add_filter('wp_title', set_page_title('test'));

The inner most function will be evaluated first, passing its return results to the next outer function.

So...

add_filter('wp_title', set_page_title(other_function(function() {return "real_filter";})));

Would pass the string 'real_filter' to add_filter assuming that all functions between the two pass their input parameters as their return value.

I believe what you would want would be something along the lines of:

function my_title_filter($old_title, $sep, $seplocation) {
    // Add some code to determine if/what you want to change your title to.

    global $brand;

    $title = "Designer " . $brand['name'] . " $sep " . get_bloginfo('name');

    return $title;
}

add_filter('wp_title', 'my_title_filter', 10, 3); // 10 is the priority,
    // and 3 is the number of arguments the function accepts.
    // wp_title can pass 3.

An additional caveat... The filter must be initialized before apply_filters is called. And best practice is to have your function defined prior to calling add_filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.