Hello,
Thank you for reaching out to Rank Math and sharing your query with us. We are happy to help you with adding a custom field to the product review schema.
To do this, you will need to use the rank_math/snippet/rich_snippet_product_entity
filter hook that allows you to modify the product schema data. You can find more information about this filter hook here.
Here is an example code snippet that you can use to add a “Check price” button with a URL to the product schema:
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
// Add your custom field name and value here
$entity['checkPrice'] = array(
'@type' => 'Offer',
'name' => 'Check price',
'url' => 'https://example.com/product-url'
);
return $entity;
});
You can add this code snippet to your child theme’s functions.php file or use a plugin like Code Snippets to add it to your site. Please make sure to replace the field name and value with your own.
Once you add the code snippet, you can test your product schema using Google’s Rich Results Test tool and see if the “Check price” button appears in the schema.
We hope this helps you achieve your goal. If you have any further questions or issues, please feel free to contact us again.
Thank you for choosing Rank Math. Have a nice day!
But this created the same url for all posts, ni? I would want just the field showing in all posts that have the product review schema activated in rankmath.
Can you help with that?
Thanks
Tom
Hello,
Thanks for your follow-up question.
The code snippet that we shared with you is just an example of how you can use the filter hook to modify the product schema data. You can customize it according to your needs and preferences. For instance, instead of using a fixed URL for the “Check price” button, you can use a function that gets the URL of the product dynamically. One way to do this is to use the get_permalink
function that WordPress provides. This function returns the URL of any post or page based on its ID. You can find more information about this function here.
To use this function, you need to pass the product ID as an argument. You can get the product ID from the global $product
variable that WooCommerce sets on the single product page. You can access this variable inside your custom function and use it with the get_permalink
function. Here is another example of how you can do this:
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
// Get the global product variable
global $product;
// Get the product ID
$product_id = $product->get_id();
// Get the product URL using the get_permalink function
$product_url = get_permalink( $product_id );
// Add your custom field name and value here
$entity['checkPrice'] = array(
'@type' => 'Offer',
'name' => 'Check price',
'url' => $product_url
);
return $entity;
});
Please make sure to test your code before applying it to your live site.
Alternatively, if you want a more seamless and user-friendly way to add custom fields to your product schema, you can upgrade to Rank Math PRO and use our Custom Schema Generator feature. This feature allows you to edit old fields, add new fields, and create completely new schemas using a simple interface. You can also use dynamic variables to populate your schema fields with relevant data from your site. You can learn more about this feature here.
We hope this helps you achieve your goal. If you have any further questions or issues, please feel free to contact us again. Thank you for choosing Rank Math as your SEO plugin. Have a nice day!
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.