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;
}
add_filter( 'pre_get_document_title', 'filter_document_title' );
function filter_document_title( $title ) {
$title="The title";
return $title;
}