1

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?

  • 1
    As far as I remember there were some changes in 4.4 regarding 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. – denis.stoyanov Feb 23 '16 at 21:25
  • 1
    Bingo! My research indicated the function was deprecated (then not, as you said) but even the new replacement supposedly use the same hook to change the title. Seems quite a few sources around the web need to be updated with this method. Thanks! – Bob Diego Feb 23 '16 at 21:37
  • I have to say, I'm confused - I thought when something was merely deprecated, it typically still worked, but was discouraged. Also, WordPress is developed with a strong preference for backward compatibility. Has this method really gone away so suddenly? – Bob Diego Feb 25 '16 at 13:05
1

We have found this for shortcode support in titles (both header and post title):

//shortcode support in titles
add_filter( 'the_title', 'do_shortcode' );      //should be post title
add_filter( 'wp_title', 'do_shortcode' );       //should be HTML/Browser title
add_filter( 'document_title_parts', 'wp44_header_title_function' );   //own function for HTML/Browser title
function wp44_header_title_function($title) {
    if (isset($title['title'])) $title['title'] = do_shortcode($title['title']);
    if (isset($title['page'])) $title['page'] = do_shortcode($title['page']);
    if (isset($title['tagline'])) $title['tagline'] = do_shortcode($title['tagline']);
    if (isset($title['site'])) $title['site'] = do_shortcode($title['site']);
    return $title;
}

I called it wp44 because in /wp-includes/general-template.php it says that this was added in 4.4 to "Filter the document title before it is generated."

I think the standard wp_title filter should work but I know we're using a crazy theme that does its own thing and looks like for that theme at least document_title_parts is the way to go.

  • document_title_parts is no official core filter. And I am not sure you really want shortcodes inside wp_title in the <head> section. – kaiser Aug 30 '16 at 15:15
  • 1
    I was under the impression it was added in 4.4 from developer.wordpress.org/reference/hooks/document_title_parts which is an official list of hooks and filters. As for the do_shortcodes, I agree it would be rare but we have a use for it doing some A/B testing where we want to show the same page but have different keywords including the title changed out. It was more for showing how to hook into the different parts of the new filter and I should have explained that better. – Andrew T Aug 31 '16 at 18:48
  • Oh, good find. Well, here goes your upvote. Anyway, I am not sure I would want to have a shortcode (Regex) in the <head> and on top of it a shortcode. For A/B testing I would filter it and work with something that does not need the DB or would at least fetch plain and not regexed-content. But that's just taste. – kaiser Sep 1 '16 at 5:57
1

If anyone else is having problems with this, it may be due to the Yoast plugin. Use:

add_filter( 'pre_get_document_title', function( $title ){
    // Make any changes here
    return $title;
}, 999, 1 );

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.