How can I use add_filter for a specific page template?

In WordPress I have a page template called designers.php.

When loading, it reads the slug to get a uniqe ID, then calls the DB to retrieve designer information.
I want to use this information to alter the page title, using the designer name in the title tag.

Read More

I’ve tried using the add_filter in my designers.php file, but it’s not working:

add_filter('wp_title', 'set_page_title');

function set_page_title($title) { 
  global $brand;
  return 'Designer '.$brand['name'].' - '.get_bloginfo('name');  
}    

I’m, guessing the add_filter must either be located inside a plugin or in functions.php file.

How can I achieve what I’m trying to do?

UPDATE
The function is never fired as long as I use wp_title. If I change it to init (for testing), the function is fired.

So why does the add_filternor work for wp_title?

Related posts

Leave a Reply

2 comments

  1. You are almost right. The filter must reside in function.php, and is called to modify the title. You can add the filter conditionally. Use this function is_page_template() to determine if wordpress is rendering your template

    Try to modify your function like this:

    add_filter('wp_title', 'set_page_title');
    
    function set_page_title($title) { 
      global $brand;
      if (is_page_template('designer.php')) 
         return 'Designer '.$brand['name'].' - '.get_bloginfo('name');   
      else
         return $title;
    }
    
  2. First of all add_filter must either be located inside a plugin or in functions.php file.

    Then, maybe you have to set the priority for the filter :

    add_filter('wp_title', 'set_page_title',1);
    
    function set_page_title() { 
      global $brand;
      return 'Designer '.$brand['name'].' - '.get_bloginfo('name');  
    }
    

    And check the <title> meta in your header.php theme.

    <title><?php wp_title(); ?></title>