4

Can i add the itemprop to the title element and still use wp_head() and add_theme_support( "title-tag" )?

I want to create a theme and get it approved on wordpres.org which uses microdata.

The preceding mean i want the HTML code looks as follows:

<html itemscope itemtype="http://schema.org/WebPage" lang="en-US">
....
        <title itemprop="name">WordPress Weblog Theme &#8211; Just another WordPress site</title>
....

Now i do not use add_theme_support( "title-tag" ) and use the following code in header.php?

<title itemprop="name"><?php echo wp_get_document_title(); ?></title>
<?php wp_head(); ?>

Now the theme check plugin says:

REQUIRED: The theme needs to have a call to wp_title(), ideally in the header.php file. REQUIRED: The theme needs to have tags, ideally in the header.php file. 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.

  • so you can just do <title itemprop="name"><?php wp_title(); ?></title>? – majick May 30 '16 at 5:21
  • @majick Not if you want to go the way WP is recommending with adding theme support for built in title tags – Tim Malone May 30 '16 at 8:24
  • 1
    A workaround could be to unhook _wp_render_title_tag from the wp_head and hook instead a custom version of _wp_render_title_tag with the title attribute, but I don't think that would be considered best practice for a public theme ;-) – birgire May 30 '16 at 10:25
  • 1
    that's a valid point @majick but I don't know if it would pass the wordpress.org theme review ;-) Maybe I will post it as an alternative workaround later today, ;-) – birgire May 30 '16 at 10:45
  • 1
    Should we make a trac for this one? The html5 specs allow aria roles for the title tag and its strange WP wouldn't support that. – cjbj May 30 '16 at 16:27
4

Since all _wp_render_title_tag does is check for title-tag theme support and wrap in <title> tags, there is really no reason why your existing implementation "shall not pass", since the proper implementation is already identical via:

<title itemprop="name"><?php echo wp_get_document_title(); ?></title>

when _wp_render_title_tag does:

echo '<title>' . wp_get_document_title() . '</title>' . "\n";

(since Theme Check is a guideline check, so what if it can't tell that a standard actually has been followed, that should in theory not stop it from passing?)

But in any case, you can get around this and improve the existing implementation at the same time by adding a customization override filter... by unhooking the existing action (as suggested by @birgire) and (my addition) hooking a wrapping function that calls _wp_render_title_tag and applies the filter to it:

if (has_action('wp_head','_wp_render_title_tag') == 1) {
    remove_action('wp_head','_wp_render_title_tag',1);
    add_action('wp_head','custom_wp_render_title_tag_filtered',1);
}

function custom_wp_render_title_tag_filtered() {
    if (function_exists('_wp_render_title_tag')) {
        ob_start(); 
        _wp_render_title_tag(); 
        $titletag = ob_get_contents();
        ob_end_clean();
    } else {$titletag = '';}
    return apply_filters('wp_render_title_tag_filter',$titletag);
}

Since it's better practice to have a filter available anyway....... Then you can add your own use case customizations easily by using the new filter:

add_filter('wp_render_title_tag_filter','custom_wp_render_title_tag');

function custom_wp_render_title_tag($titletag) {
    $titletag = str_replace('<title>','<title itemprop="name">',$titletag);
    return $titletag;
}

Of course it would be much cleaner if the core function was simply updated to:

function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }
    echo apply_filters( 'wp_render_title_tag' , '<title>' . wp_get_document_title() . '</title>' . "\n" );
}
  • +1 for being a smartass, but I still wouldn't recommend doing it like this in a public theme, because it is not future proof. WP might change the workings of the private function and the theme could break. – cjbj May 30 '16 at 12:45
  • 1
    you're right could be a little better there... I updated the code to add filter priority match and function check, unique function names... apart from that I don't see how a passthrough function wrapper like this could break anything... as an override it will either be triggered or not... – majick May 30 '16 at 13:30
  • Since wp_head is an action, we could use has_action for that context, even though it's just a wrapper for has_filter ;-) – birgire May 30 '16 at 14:43
  • i see you ran has_typo on the code... fixed. ;-) – majick May 30 '16 at 16:46
  • This is not working anymore (as of WordPress 4.8). The <title> tag gets removed completely. – Ciprian Jul 24 '17 at 12:02
1

Unfortunately echoing the <title> tag is currently hardwired in general-template.php (line 1062). It's in a private function, meaning that you cannot modify or overrule it. So, at the moment you cannot modify the tag. You might want to issue a trac to ask that they support this in the future.

  • 2
    but but... the impossible is achieved by those who think it possible... :-) – majick May 30 '16 at 11:43

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.