0

I am running on WP 4.7.2 and Yoast SEO 4.4. I want to append a PHP variable to all titles. But it seems I am not able to do that.

I have read that wp_title used to be the method before WP 4.4, then it was removed, and then it was returned because of some 'bug'?

I have tried using the wpseo+add_filter method, but it does not set the site title.

I have searched /wp-content/plugins/wordpress-seo via grep -r '<title>' * and am not seeing where the plugin sets the title tag. It did return few lines, but none of them seem to be related.

I found the following code in clss-frontend.php:

// When using WP 4.4, just use the new hook.
add_filter( 'pre_get_document_title', array( $this, 'title' ), 15 );
add_filter( 'wp_title', array( $this, 'title' ), 15, 3 );
3

There's a wpseo_title filter you can hook into. Example:

add_filter('wpseo_title', 'add_to_page_titles');
function add_to_page_titles($title) {
    $title .= $addToTitle;
    return $title;
}
  • Thank you. I have tried that one as well, but it is not working for me. I have added that exact code in the theme's header.php file right after the wp_head() call in the header. No change happens. – KingsInnerSoul Feb 28 '17 at 20:35
  • 1
    There's the problem - if you are putting this in the theme, it belongs in functions.php, not header.php. The title is set within wp_head() so you are trying to filter it after it has already been set and output as HTML - too late. – WebElaine Feb 28 '17 at 20:47
  • That was easy!!! Thank you for pointing me in the right direction. – KingsInnerSoul Mar 1 '17 at 16:27
2

Where does Yoast SEO plugin sets the site/page title?

Yoast SEO plugin adds two hooks that filter the title.

add_filter( 'pre_get_document_title', array( $this, 'title' ), 15 );
add_filter( 'wp_title', array( $this, 'title' ), 15, 3 );

The pre_get_document_title hook was added in WordPress 4.4, so on older versions, that hook will never get fired.

For versions older than 4.4, the wp_title hook will generate the SEO title.
For versions newer than 4.4, the pre_get_document_title hook will generate the SEO title.

The wp_title hook will still fire on versions newer than 4.4, but it won't do anything because if we look at the WPSEO_Frontend::title() method, notice that if the the $title property is already set, it will simply return that. If it's not set, it will generate the title using the WPSEO_Frontend::generate_title() method.

How to change the SEO title?

To modify the SEO title, you need to hook in after Yoast generates the title, but before it's output. The latest that Yoast SEO could generate the title is on the wp_title hook at a priority of 15. The title is output after all the wp_title hooks are processed. So we need to hook into wp_title after 15.

If you want to change the title from within another plugin, just add the following code to your plugin:
If you want to change the title from within your theme, add the following toyour functions.php file:

//* Hooking after priority 15 guarantees that Yoast SEO has generated the title
add_filter( 'wp_title', 'wpse_258323_title', 20, 2 );
function wpse_258323_title( $title, $post_id ) {
  //* Do something with the $title
  return $title;
}

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.