3

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

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'));
}
1
2

You may want to filter out the title.

add_filter( 'wp_title', 'wpse179527_wp_title' );
function wpse179527_wp_title( $title ) {
  global $post;
  if ( is_single() && 'custom-post' == get_post_type( $post ) )
    return '';
  return $title;
}
2

I posted this answer for another question but since it is relevant and more up-to-date, I though it might be useful for some people.

How document title is generated has changed since Wordpress v4.4.0. Now wp_get_document_title dictates how title is generated:

/**
 * 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";
}

Here is the code from v5.4.2. Here are the filters you can use to manipulate title tag:

function wp_get_document_title() {
    /**
    * Filters the document title before it is generated.
    *
    * Passing a non-empty value will short-circuit wp_get_document_title(),
    * returning that value instead.
    *
    * @since 4.4.0
    *
    * @param string $title The document title. Default empty string.
    */
    $title = apply_filters( 'pre_get_document_title', '' );
    if ( ! empty( $title ) ) {
        return $title;
    }
    // --- snipped ---
    /**
    * Filters the separator for the document title.
    *
    * @since 4.4.0
    *
    * @param string $sep Document title separator. Default '-'.
    */
    $sep = apply_filters( 'document_title_separator', '-' );

    /**
    * Filters the parts of the document title.
    *
    * @since 4.4.0
    *
    * @param array $title {
    *     The document title parts.
    *
    *     @type string $title   Title of the viewed page.
    *     @type string $page    Optional. Page number if paginated.
    *     @type string $tagline Optional. Site description when on home page.
    *     @type string $site    Optional. Site title when not on home page.
    * }
    */
    $title = apply_filters( 'document_title_parts', $title );
    // --- snipped ---
    return $title;
}

So here are two ways you can do it.

First one uses pre_get_document_title filter which short-circuits the title generation and hence more performant if you are not going make changes on current title:

function custom_document_title( $title ) {
    return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );

Second way uses document_title_separator and document_title_parts hooks for the title and the title seperator that are executed later in the function, after title is generated using functions like single_term_title or post_type_archive_title depending on the page and about to be outputted:

// Custom function should return a string
function custom_seperator( $sep ) {
   return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );

// Custom function should return an array
function custom_html_title( $title ) {
   return array(
     'title' => 'Custom Title',
     'site'  => 'Custom Site'
    );
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );
1
  • Thank you for this thorough answer! It does indeed work on WP in 2020, I couldn't get the earlier answers to do anything. – JoLoCo Jul 29 '20 at 15:11

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.