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 – 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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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" );
}
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0