Hello,
Thank you for contacting the support and bringing your concern to our attention.
1. Could you please further explain your goal here in detail? Rank Math already detects post thumbnails or featured images to use for social meta tags and schema structure.
2. You can extend the json+ld code generated by Rank Math and just add a condition statement to detect a certain page before executing it:
https://rankmath.com/kb/filters-hooks-api-developer/#extend-json-ld-data
add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
global $post;
if( $post->ID == 1) {
$data; /* Modify schema here */
}
return $data
}, 99, 2);
3. You may follow the example filter below to modify robots meta using our filter:
add_filter( 'rank_math/frontend/robots', function( $robots ) {
global $post;
if( $post->ID == 1) {
$robots['index'] = 'noindex';
}
return $robots;
});
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hi Jeremy, thanks for the quick response,
About #1, what we need is a way to get the thumbnail from RankMath, to use in our plugins and theme.
The get_the_post_thumbnail function from WP is very limited, it only gets the featured image that was manually set by the authors. If there isn’t one defined, it returns null.
Yoast has a function that also tries to get the first image from the post if there’s no featured image. That’s what we are using in our scripts.
Can i do the same with RankMath? Is there a function available to use in the theme, that returns the best thumbnail available?
Bestm
Hello,
We have a function inside our plugin called get_thumbnail_with_fallback() that tries to retrieve the post thumbnail with the following fallback structure:
1. Post thumbnail.
2. First image in content.
3. Facebook image if any.
4. Twitter image if any.
5. Default open graph image set in the options panel.
You can find that function inside the file: wp-content\plugins\seo-by-rank-math\includes\helpers\class-wordpress.php
Let us know if that is the type of function that you are looking for.
Looking forward to hearing back from you.
Hi Miguel,
Thanks! That’s exactly what we need.
Is it safe to use this function in our plugins and theme?
Best,
Hello,
Yes, it should be safe to use as you only need to provide the post ID and the second argument will be the size of the image you wish to retrieve. It shouldn’t cause any issues when calling that function. Here’s a sample code:
global $post;
$theImage = RankMath\Helper::get_thumbnail_with_fallback($post->ID)[0];
Please ensure to test it out first and conduct a backup of your website.
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hello,
We are super happy that we could address your concern. If you have any other questions in the future, know that we are here to help you.
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.