Archived topic
Disabling schema markup only for article element
7 replies · Started by Brand on March 8, 2023
All my WooCommerce single product article elements have this HTML schema markup:
itemtype="itemtype="https://schema.org/CreativeWork""
I expect this is to be a conflict with the Yoast plugin. I'd like to keep all the schema markup added by GP such as the navigation elements, but not the schema markup for articles, as I want this to be handled by the Yoast plugin.
I've tried:
add_filter( 'generate_article_itemtype', 'itemtype' );
function itemtype( $type ) {
return '';
}
Which just returns this in the HTML:
itemtype="itemtype="https://schema.org/""
I also tried:
add_filter( 'generate_schema_type', '__return_false' );
Which removes all the markup completely.
If it helps, I'd like to do something like this:
add_filter( 'generate_schema_type', 'itemtype' );
function itemtype( $element ) {
if( is_product() && $element == 'article' ) {
return false;
}
}
However, the parameter in generate_schema_type holds the value microdata. Can't find a use for this to build some logic that disables the schema markup for the article element.
If anyone stumbling upon this knows how to disable the schema markup on WooCommerce single product articles through Yoast, that would also suffice. But I'd prefer to just disable it in GP.
Hi there,
try this:
add_filter( 'generate_article_microdata',function( $data ) {
if( is_product() ) {
return false;
}
return $data;
} );
For more detail you can find the filter here:
https://github.com/tomusborne/generatepress/blob/4895a2e7595bb809075b375201fd735112f41570/inc/theme-functions.php#L500
Hi David, thanks for the quick reply.
Unfortunately this doesn't seem to make any change.
The output is still the same: itemtype="itemtype="https://schema.org/CreativeWork"".
I disabled a lot of the installed plugins, including Yoast, but the issue persisted.
I finally managed to find the issue, which is caused by a function in themes/generatepress/inc/class-html-attributes.php.
public function woocommerce_content( $attributes ) {
if ( is_singular() ) {
$attributes['id'] = 'post-' . get_the_ID();
$attributes['class'] = esc_attr( implode( ' ', get_post_class( '', get_the_ID() ) ) );
if ( 'microdata' === generate_get_schema_type() ) {
$type = apply_filters( 'generate_article_itemtype', 'CreativeWork' );
$attributes['itemtype'] = sprintf(
'itemtype="https://schema.org/%s"',
$type
);
$attributes['itemscope'] = true;
}
} else {
$attributes['class'] = 'woocommerce-archive-wrapper';
}
return $attributes;
}
If I modify $attributes['itemtype'] so it looks like this:
if ( 'microdata' === generate_get_schema_type() ) {
$type = apply_filters( 'generate_article_itemtype', 'CreativeWork' );
$attributes['itemtype'] = 'https://schema.org/' . $type;
$attributes['itemscope'] = true;
}
And using this filter:
add_filter( 'generate_article_itemtype', function( $type ){
return 'WebPage';
});
It gives me the correct output in the HTML:
itemtype="https://schema.org/WebPage" itemscope
Is there any way I can make these changes without having to go into the core files? And is this an error with the plugin itself, or perhaps a conflict with another plugin?
Hmmm.... you should not need to modify the theme functions for this to work:
add_filter( 'generate_article_itemtype', function($type){
if( is_product() ) {
return 'webPage';
}
},99 );
I added the priority just in case but i didn't need it on my test site.
That filter works for changing the itemtype, but the markup itself was still messed up. The filter would give this output:
itemtype="itemtype="https://schema.org/WebPage""
The woocommerce_content() function was returning the wrong output on WooCommerce single product pages. I suspect this might be an issue with the GeneratePress theme itself.
$attributes['itemtype'] = sprintf(
'itemtype="https://schema.org/%s"',
$type
);
to $attributes['itemtype'] = 'https://schema.org/' . $type; fixed it for me.
Hmmm... that was a bug we fixed here:
https://github.com/tomusborne/generatepress/issues/457
Is the theme up to date ?
It's not. But I'll look into that ASAP :) thank you for your time David
You're welcome. Let us know how you get on.