add_theme_support( ‘title-tag’ ) in conflict with custom titles function

‘Title-tag’ is a theme feature introduced in Version 4.1, and I want to use it as the default title of my theme. This feature should be added on the after_setup_theme or init action. It is recommended here. The use of this feature works perfect for me. BUT:

Moreover, I have a custom titles function which can be enabled/disabled using the Option-Tree framework. This framework is loaded directly in functions.php. Then, I have the rest of the functions added on the after_setup_theme with priority 2:

add_action( 'after_setup_theme', 'theme_functions', 2);

And this is the filter added for my custom titles:

add_filter( 'wp_title', 'custom_titles', 10, 2 );

When I disable my custom_titles() function, the new feature title-tag works perfect, but when I enable the custom ones, it returns the title twice. Exactly that:

<title>Front page meta title example</title>

Front page meta title example<title></title>

The first one is correct, and it’s using the wp_title() function inserted in the header.php between the title tags, but the second one it’s included at the first line of the wp_head() function.

In fact, my custom_titles() function worked perfect without the use of the new feature title-tag. So it seems the conflict appears when I try to use both.

Do you think it’d be a good solution to put both features in after_theme_setup priority 1 in a simple conditional statement, or is there a better way for doing that? It seems the solution should be easier.


UPDATE: This is crazy. If I remove the title tags of the header to use the new feature, the theme-check returns:

REQUIRED: The theme needs to have tags, ideally in the
header.php file.

If I remove the new feature to use my custom function, the theme-check returns:

RECOMMENDED: No reference to add_theme_support( “title-tag” ) was
found in the theme. It is recommended that the theme implement this
functionality for WordPress 4.1 and above.

To be honest: LOL. Adding add_theme_support in functions.php, and title tags in header.php as they «RECOMMEND» & «REQUIRE», respectively, it returns always the title twice!

2 Answers
2

Your problem is that you can not use wp_title() in the theme if the theme already supports title-tag. The <head> of your theme should look like this:

<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <?php wp_head(); ?>
</head>

The filter and title-tag support:

add_action( 'after_setup_theme', 'theme_functions' );
function theme_functions() {

    add_theme_support( 'title-tag' );

}

add_filter( 'wp_title', 'custom_titles', 10, 2 );
function custom_titles( $title, $sep ) {

    //Check if custom titles are enabled from your option framework
    if ( ot_get_option( 'enable_custom_titles' ) === 'on' ) {
        //Some silly example
        $title = "Some other title" . $title;;
    }

    return $title;
}

If you do this, it will work perfectly.

Leave a Comment