1

Since WP4.1, there is a new 'title-tag' theme feature to add the title tag. I'm reading an article here to add itemprop to the <title> tag so that it will look like this : <title itemprop="name"><?php wp_title(''); ?></title> but using this new theme feature, the title tag is added automatically to the header.

I wonder what is the best way to add itemprop to <title> when using add_theme_support('title-tag'), considering that the title is not written manually in the header.php file anymore?

Thanks!

2

If your theme is using add_theme_support('title-tag'), then you can try the following:

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

and then just hook in your own modified version:

add_action( 'wp_head', 'wpse_render_title_tag_with_itemprop', 1 );

function wpse_render_title_tag_with_itemprop() {

    if ( did_action( 'wp_head' ) || doing_action( 'wp_head' ) ) 
    {     
        printf( 
            '<title itemprop="name">%s</title>' . PHP_EOL, 
             wp_title( '|', false, 'right' )
        );
    }
}

with the title tag containing the itemprop attribute.

Note: The current_theme_supports( 'title-tag' ) is using debug_backtrace() to check if it was called within the _wp_render_title_tag() or wp_title() functions:

if ( 'title-tag' == $feature ) {
    // Don't confirm support unless called internally.
    $trace = debug_backtrace();
    if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
        return false;
    }
 }

Also note that if we would have used:

add_action( 'after_setup_theme', function() {
        remove_theme_support( 'title-tag' );
}, 11 );

which is equivalent to:

global $_wp_theme_features;
unset( $_wp_theme_features['title-tag'] );

then the following part of wp_title() would be excluded:

if ( current_theme_supports( 'title-tag' ) && ! is_feed() ) {
    $title .= get_bloginfo( 'name', 'display' );

    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) ) {
        $title .= " $sep $site_description";
    }

    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
        $title .= " $sep " . sprintf( __( 'Page %s' ), max( $paged, $page ) );
    }
}

The title tag could therefore become empty on the front-page, for example.

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.