I am having difficulty overriding the title set in the admin panel for a custom template page, and outputting a custom <title>
tag.
The parent theme is WordPress's stock Twentysixteen, which uses the title-tag theme feature (as opposed to the soon-to-be deprecated function wp_title() ). According to WordPress Code Reference, the correct hook is the wp_title
filter:
The wp_title filter is used to filter the title of the page (called with wp_title()). This filters the text appearing in the HTML tag (sometimes called the “title tag” or “meta title”), not the post, page, or category title.
So I should be able to simply create a conditional test in my functions.php file and override the title tag created by WordPress there, e.g.:
function custom_filter_wp_title( $title, $sep ) {
// removed conditional to prove not working anywhere
// if ( is_page_template( 'sometemplate.php' ) ) {
$title = "My custom template page...";
// }
return $title;
}
add_filter( 'wp_title', 'custom_filter_wp_title', 10, 2 );
Derived from Codex example.
As far as I can tell, this custom filter is working nowhere, the title set in the back end is appearing in the HTML header's title tag. I have elevated the priority to 99999, still nothing. What am I doing wrong?
wp_title()
. First it was deprecated then it was not or something like this. Anyway check the following filters:document_title_parts
,document_title_separator
,pre_get_document_title
.