Is there a way to override the tag specified in header.php?

Firstly this is neither an SEO question nor a question about changing the title tag sitewide. If you google my question those are all the answers you see.

So we have our own theme and we have complete control over header.php. We know how to set the title. Currently it looks like this:

<head>
    <title><?php wp_title(' | ', true, 'right'); bloginfo('name'); ?></title>
etc...

Nope, the problem is this. For most pages we want the title to show as above. It’s just that we’ve realised that for one particular custom post type (and its associated template) the CPT’s title shouldn’t appear publicly. It’s for admin use only. Strange, but there you go. We don’t show it anywhere in the template’s H1, content, etc.

But it is showing in the title.

Ideally we’d like a way to override the header.php title from within the template, to specify an alternative title just for this particular set of pages. Is that possible?

4 s
4

First, let’s change your <title> to

<title><?php wp_title(' | ', true, 'right'); ?></title>

Because adding to the title string in that was isn’t very future-forward, instead it’s best to use a filter to do any modifications to the title. So let’s instead ad (in functions.php):

add_filter('wp_title', 'my_custom_title');
function my_custom_title( $title )
{
    // Return my custom title
    return sprintf("%s %s", $title, get_bloginfo('name'));
}

Then let’s extend this handy little title filter to do what you’re wanting to do:

add_filter('wp_title', 'my_custom_title');
function my_custom_title( $title )
{
    if( is_singular("your_post_type"))
    {
        return ""; // Return empty
    }
    // Return my custom title
    return sprintf("%s %s", $title, get_bloginfo('name'));
}

Leave a Comment