wp_title filter takes no effect on the <title> tag
Not sure if its necessary to inject the variable, but try this.
function change_the_title($title) {
return 'My modified title';
}
add_filter('wp_title', 'change_the_title');
I finally found out that the WordPress core code was changed, see the below piece of code.
/**
* Displays title tag with content.
*
* @ignore
* @since 4.1.0
* @since 4.4.0 Improved title output replaced `wp_title()`.
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
So, after 4.4, the core do not inject the wp_title
result into the header <title>
tag, but do the same thing with a new function wp_get_document_title
.
So instead, we can do the same thing by:
1. change the title directly:
add_filter('pre_get_document_title', 'change_the_title');
function change_the_title() {
return 'The expected title';
}
2. filtering the title parts:
add_filter('document_title_parts', 'filter_title_part');
function filter_title_part($title) {
return array('a', 'b', 'c');
}
For more, see the details here: https://developer.wordpress.org/reference/functions/wp_get_document_title/
PS: Looking into the source of function
wp_get_document_title
is a good idea, the hooks inside which tells a lot.