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