wp_title filter takes no effect on the <title> tag

15,647

I finally found out that the WordPress core code was changed, see the below piece of code.

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

So, after 4.4, the core do not inject the wp_title result into the header <title> tag, but do the same thing with a new function wp_get_document_title.

So instead, we can do the same thing by:

1. change the title directly:

add_filter('pre_get_document_title', 'change_the_title');
function change_the_title() {
    return 'The expected title';
}

2. filtering the title parts:

add_filter('document_title_parts', 'filter_title_part');
function filter_title_part($title) {
    return array('a', 'b', 'c');
}

For more, see the details here: https://developer.wordpress.org/reference/functions/wp_get_document_title/

PS: Looking into the source of function wp_get_document_title is a good idea, the hooks inside which tells a lot.

Share:
15,647

Related videos on Youtube

Alfred Huang
Author by

Alfred Huang

一切有为法, 如梦幻泡影。 如露亦如电, 应作如是观。

Updated on September 27, 2022

Comments

  • Alfred Huang
    Alfred Huang over 1 year

    I just added the following filter in my theme functions.php file:

    function change_the_title() {
        return 'My modified title';
    }
    add_filter('wp_title', 'change_the_title');
    

    And in my header.php:

    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta id="viewport" name="viewport" content="width=device-width">
        <link rel="profile" href="http://gmpg.org/xfn/11">
        <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
        <?php wp_head(); ?>
    </head>
    <body <?php body_class();?>>
    

    Then, I found the title of my page did NOT change! And the title tag was injected in the wp_head function.

    More, if I call the function wp_title manually in the header, it does return the expected value.

    What's the matter? How can I work around it?


    Addition: My WordPress version is 4.4.

  • Alfred Huang
    Alfred Huang over 8 years
    Thank you, but I finally found the solution myself.
  • Alfred Huang
    Alfred Huang over 8 years
    Thank you, but I finally found the solution myself.
  • Omer Farooq
    Omer Farooq over 8 years
    Great, didn't knew about it :)
  • Alfred Huang
    Alfred Huang over 8 years
    We don't need to do so, because the core function wp_head() will do the injection.
  • wesamly
    wesamly about 8 years
    Thank you @fish_ball just perfect answer.
  • Ryan Marshall
    Ryan Marshall almost 8 years
    FINALLY! Every other post about this topic is wrong- nailed it! Thanks!
  • Jonathan Gruber
    Jonathan Gruber almost 8 years
    Man... Sometimes I hate WordPress. Why was this so hard to find?
  • Alfred Huang
    Alfred Huang almost 8 years
    @JonathanGruber be a contributor, and vote me up, then you will love it again, haha!
  • Adeel Raza
    Adeel Raza almost 8 years
    pre_get_document_title does not work for some themes., any idea why. ?
  • Colin
    Colin almost 8 years
    @Adeel I think the theme has to use the new function add_theme_support( 'title-tag' ) rather than the old function wp_title
  • Desper
    Desper almost 3 years
    patta much obliged mate. have been struggling with deprecated wp_title till now