Created
July 28, 2023 07:42
-
-
Save khoipro/469a00a2565b9750c123a25ea4e2d0b9 to your computer and use it in GitHub Desktop.
Fix Rankmath pro - product category schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('rank_math/json_ld', 'codetot_product_rich_snippet_schema', 100); | |
/** | |
* Fix RankMath missing `AggregateRating` on product category schema | |
* | |
* @author codetot | |
*/ | |
function codetot_product_rich_snippet_schema( $data ) { | |
if ( !is_tax( 'product_cat' ) ) { | |
return $data; | |
} | |
if ( isset( $data['ProductsPage']) ) { | |
$product_schemas = array_map(function($product) { | |
if ( empty($product['aggregateRating'] ) ) { | |
$aggregateRating = [ | |
'@type' => 'AggregateRating', | |
'ratingValue' => 5, | |
'ratingCount' => 1 | |
]; | |
$product['aggregateRating'] = $aggregateRating; | |
} | |
return $product; | |
}, $data['ProductsPage']['@graph'] ); | |
$data['ProductsPage']['@graph'] = $product_schemas; | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment