Last active
August 18, 2021 13:18
-
-
Save obiPlabon/382b97935c107622a8db2a31f7d874a3 to your computer and use it in GitHub Desktop.
Add RankMath ratingValue schema support to Directorist
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', function( $schemas, $jsonld ) { | |
if ( ! ( defined( 'ATBDP_POST_TYPE' ) && is_singular( ATBDP_POST_TYPE ) ) ) { | |
return $schemas; | |
} | |
foreach ( $schemas as $key => &$schema ) { | |
if ( is_array( $schema['@type'] ) ) { | |
continue; | |
} | |
$type = strtolower( $schema['@type'] ); | |
// You can more support extending the array | |
if ( ! in_array( $type, array( 'product' ), true ) ) { | |
continue; | |
} | |
if ( isset( $schema['review'], $schema['review']['reviewRating'] ) ) { | |
$schema['review']['reviewRating'] = array_merge( | |
$schema['review']['reviewRating'], | |
array( | |
'ratingValue' => ATBDP()->review->get_average( get_the_ID() ) | |
) | |
); | |
} | |
} | |
return $schemas; | |
}, 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment