1

I'm using a theme child of Ultra Theme. This theme is using this :

add_theme_support( 'title-tag' );

I'd like to customize the title tag of all posts & pages, here is my code :

add_filter( 'wp_title', 'my_custom_title', 10, 2);
function my_custom_title() {
    return("Foo bar");
}

The code is not working and I can't figure out why !

4
  • You need to determine whether the theme is the one having the final say. So, you could switch to a default theme, add your function as a plugin, and see whether your function now overrides the title. If not, you have just determined that a plugin is affecting things. SEO plugins often affect the title. If you do find that your function does override the title when you're working with a default theme, then you know for sure it's the theme you are using, and you can search through its files for "title" to narrow it down. – WebElaine Jun 5 '18 at 14:55
  • Try a higher priority than 10 for the filter like 100000. – Nathan Johnson Jun 5 '18 at 16:26
  • @WebElaine i've switched to twentysixteen, added my code to the theme's functions.php and disabled all plugins : the problem remains ! – Sami Jun 7 '18 at 13:51
  • @NathanJohnson it doesn't work :( – Sami Jun 7 '18 at 13:55
6

When adding title-tag support in a theme, the title tag can be filtered by several filters, but not wp_title. The reason is that if the theme supports title-tag, WordPress uses wp_get_document_title() instead of wp_title().

For themes with support for title-tag you can use document_title_parts:

add_filter( 'document_title_parts', 'filter_document_title_parts' );
function filter_document_title_parts( $title_parts ) {

    $title_parts['title'] = 'The title'; 
    $title_parts['tagline'] = 'A tagline';
    $title_parts['site'] = 'My Site';

    return $title_parts; 

}

Or pre_get_document_title:

add_filter( 'pre_get_document_title', 'filter_document_title' );
function filter_document_title( $title ) {

    $title = 'The title'; 

    return $title; 

}
3
  • I've removed my code and put yours (in functions.php) : it doesn't work :( – Sami Jun 7 '18 at 16:44
  • If you use the old wp_title() function to output the document title, then you must use the old wp_title filter. If you support the more recent title-tag theme feature, then you must use the new filters you see in this answer. Do not mix them. – cybmeta Jun 7 '18 at 18:16
  • Your Approach helped solve my issue, Thanks alot – Simo Patrek Nov 5 '19 at 2:37
0

This seems to be the problem's root :

add_theme_support( 'title-tag' );

In functions.php of the child theme I've added this to cancel that function :

add_action( 'after_setup_theme', 'remove_featured_images_from_child_theme', 11 ); 
function remove_featured_images_from_child_theme() {
    remove_theme_support( 'title-tag' );
}

Then I've added this to header.php :

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

With this two snippets of code the wp_title filter works fine.

But I think this is not the right way since WP recommands to use add_theme_support('title-tag'), but I'm disabling it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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