Hello,
Thank you for contacting Rank Math today.
To modify existing schema type, in your case article schema, you can would need to use the filter below:
/**
* Filter to change the schema data.
* Replace $schema_type with schema name like article, review, etc.
* @param array $entity Snippet Data
* @return array
*/
add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {
return $entity;
});
Looking forward to helping you. Thank you.
Hello,
Thank you for the reply.
I’ve added the following code as per your suggestion with the modifications I THINK might work. Again, I am no coder, I have a very basic idea of how any of this works.
This is my code:
add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
return $entity['thumbnailURL'] = '$featured_img_url';
return $entity;
});
However, once emptying all caches and checking an article link in the structured data testing tool, the “thumbnailURL” does not appear in the Schema Markup.
Am I doing something wrong here with the code?
Please advise.
Hello,
You need to replace {$schema}
in this line:
add_filter( "rank_math/snippet/rich_snippet_{$schema}_entity", function( $entity ) {
With the schema you want to modify. For example, the Article schema would be like this:
add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
Looking forward to help you.
Hello Alberto,
I’ve made the changes as you suggested. Here is the code I used:
add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
return $entity['thumbnailURL'] = '$featured_img_url';
});
When I ran the structured data testing tool, ALL of the rank math article schema was gone and was replaced just by “$featured_img_url”.
I do NOT want to replace everything, I just want to add to the existing schema a single line of ‘thumbnailURL’ = featured image URL.
How can I ADD to the existing data and not replace everything?
Hello,
Please use the following and let us know:
add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
$entity['thumbnailURL'] = '$featured_img_url';
return $entity;
});
Looking forward to helping you. Thank you.
Hello Michael,
The code you gave was placing the “thumbnailURL” inside the article schema, but the URL was just “$featured_img_url” and not the actual URL. This is because there was extra (”) quotation marks around the parameter. After I removed them it worked.
For anyone else who might find this in the future. Here is the final code that worked.
/**
* Adds ThumbnailURL for Each Post into RankMath Schema Data
* Collect data to output in JSON-LD.
*
* @param array $unsigned An array of data to output in json-ld.
* @param JsonLD $unsigned JsonLD instance.
*/
add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
$entity['thumbnailURL'] = $featured_img_url;
return $entity;
});
-
This reply was modified 4 years, 1 month ago by Artem Rev.
Hello,
We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
Also, thank you for sharing your code, I am sure it will help a lot of people!
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.