How can I use add_filter for a specific page template?

In Wordpress, I have a page template named designers.php.

When loading, it reads slugto obtain the uniqe identifier, and then calls the database to obtain design information.
I want to use this information to change the page name using the name of the designer in the title tag.

I tried using add_filterin my designers.phpfile, but it does not work:

add_filter('wp_title', 'set_page_title');

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

I assume that it add_filtershould either be inside the plugin or in a file functions.php.

How can I achieve what I am trying to do?

UPDATE
Function never runs while I use wp_title. If I change it to init(for testing), the function will be launched.

add_filter wp_title?

+3
2

. function.php . . is_page_template(), , Wordpress

:

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

add_filter , functions.php.

, , :

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

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

<title> header.php.

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

Source: https://habr.com/ru/post/1791844/


All Articles