Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 511
Steven Avatar asked Feb 13 '11 15:02

Steven


1 Answers

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;
}
like image 92
keatch Avatar answered Sep 23 '22 17:09

keatch