1

I really can't get the right practices from official docs. I think it's a complete mess.

At some point, they started to say to activate the title-tag feature: after_setup_theme >

add_theme_support('title-tag');
  • Ok. At this point they stated we should delete the <title> tag from page and stop using wp_title(). Perfect.

  • So we had to use the 'wp_title' filter to modify our titles. Right. I did, that worked, once upon a time.

  • Then they said ''wp_title' filter was deprecated, and we should have used pre_get_document_title and other filters. Wow, great. Currently they seem not to work, though.

  • Then they said ''wp_title' filter was 'reinstated until alternative usages have been identified and a path forward for them defined' Sexy.

But if I look into the core code, I can see that the 'title-tag' has no filters to modify its output. So if I want custom coded texts inside my titles, should I use wp_title() again and disable 'title-tag' support?

Really, can someone shed a definitive light on this topic?

2 Answers 2

3

Do you have any SEO plugins installed? They usually fiddle with this logic. Having Yoast enabled, the pre_get_document_title does not work for me, instead you should use wpseo_title per their documentation.

Having said that, with SEO plugins disabled and add_theme_support('title-tag'); enabled, the pre_get_document_title filter works for me without any problem.

add_action('after_setup_theme', function () {
    add_theme_support('title-tag');
}, 20);

add_filter('pre_get_document_title', function ($title) {
    return 'ORO!';
});

Result is, that I see ORO! as the page title.

1
  • That's interesting. Commented Dec 2, 2019 at 13:52
-1

As far as I can say, currently for me the best option is the old school wp_title() thing: that will work. So:

  • Do not add title-tag theme sopport
  • Keep your <title><?php wp_title([whatever options]) ?></title> on page
  • If you need further customisations, feel free to use the wp_title filter > it's not deprecated and will correctly work.

Until the next wp messy recommendations..

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.