I followed this code but if the ACF field wasn’t set it didn’t seem to be picking up the default image in it’s place.
I’ve hard coded it like below, but how do I replace this with a variable?
// Put OG Image in ACF fields
add_filter( 'rank_math/opengraph/twitter/twitter_image', function( $attachment_url ) {
global $post;
if (get_field('og_image_large', $post->ID)) {
$attachment_url = get_field('og_image_large', $post->ID); /* Insert specific field here */
} else {
$attachment_url = 'https://mydomain.com/wp-content/uploads/OpenGraphDefault.png';
}
return $attachment_url;
});
The ideal sequence would be:
If ACF field, use that
elseif Rankmath image set for post use that
else use default image
Any idea how I do that?
Hello @the-conversion-co,
Thank you for reaching out to us.
You can remove the else state for the default image as returning the variable $attachment_url will return the default image automatically if you don’t change it. Just ensure that you have inserted a featured image to your post/page or you have set an image under Rank Math > Titles & Meta > Global Meta > OpenGraph Thumbnail.
Also, the filter you created may not work. Kindly make use of this instead:
add_filter( 'rank_math/opengraph/twitter/image', function( $attachment_url ) {
...
});
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.