2

'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!

  • A hint: When using 'title-tag' support, you can see the source code of function wp_get_document_title() - there are the following filters (WP 4.7.3): pre_get_document_title, document_title_separator and document_title_parts. The first one gives full control over the title, instead of specified filter wp_title in your question, which now seems obsolete and very soon can get deprecated. – Minister Apr 21 '17 at 7:54
6

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.

  • I see. Thanks. I knew it had to be easier... I think my mistake was on the custom titles function, without calling the $title and $sep variables. Let me try it :) – Gerard Feb 3 '15 at 16:58
  • 1
    I've added the checking of enable_custom_titles option. – cybmeta Feb 3 '15 at 16:59
  • Is it better to use the checking inside of the function? – Gerard Feb 3 '15 at 17:02
  • There's one problem: in this function I don't save the titles in variables, I use functions when it's possible, such the_title(); or single_tag_title();... And in variables when it's needed, for example in meta-boxes: $metatitle = get_post_meta($post->ID, 'metabox_title_tag', true);. How can I fix it? – Gerard Feb 3 '15 at 17:06
  • 1
    the_title(), single_tag_title() and son on are template tags, that is, functions intended to be used in template files, not to get information outside template files. For example, if you want to show the title of a post in a template file you can use the_title() inside the loop; if you want to get the title of a post outside a template file you could use get_the_tile( $post_ID ) or a post object returned by get_post, WP_Query, etc. If you need help with this, please, post another question. – cybmeta Feb 3 '15 at 17:33
3

I've found a solution, so I'm sure it will be useful for others.


First: custom titles

Explanation: If my custom_titles() function is enabled, load the function in functions.php under the after_theme_setup priority 2, and show the title tags in the header.php file.

In functions.php:

if ( ot_get_option( 'enable_custom_titles' ) === 'on' ) {
    // The custom titles function
}

In header.php:

<?php if ( ot_get_option( 'enable_custom_titles' ) === 'on' ) : ?>
<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php endif; ?>

<!-- The next will return the new WP titles feature if custom titles are off -->
<?php wp_head(); ?>

Then: title-tag support

Explanation: If my custom_titles() is disabled, add_theme_support('title-tag'); in functions.php under the after_theme_setup priority 1.

So, in functions.php:

if ( ot_get_option( 'enable_custom_titles' ) === 'off' ) {
    add_theme_support( 'title-tag' );
}

About the after_theme_setup priority, in my case it's just needed to load the Option-Tree framework before to use the ot_get_option() function, so it could be different —not needed— if someone uses a different options framework.

Now my theme has the new titles feature in Version 4.1 and custom titles for SEO using meta-boxes: both available. (I know it is plugin territory, but it's nice to have some essential features —«essential» in my opinion— integrated in the theme). Thanks to this, the clients will have more control on how titles are displayed.

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.