Rank Math Fallback Open Graph Image by Post Type
Supplies a fallback Open Graph image for chosen post types when Rank Math finds no social or featured image on a post.
Opens WPIDE on
Code
// Post type => fallback image URL.
$wpide_rm_og_fallbacks = array(
'post' => home_url( '/wp-content/uploads/social-blog.jpg' ),
);
add_filter( 'rank_math/opengraph/facebook/image', function ( $image ) use ( $wpide_rm_og_fallbacks ) {
if ( $image || ! is_singular( array_keys( $wpide_rm_og_fallbacks ) ) ) {
return $image;
}
return $wpide_rm_og_fallbacks[ get_post_type( get_queried_object_id() ) ];
} );
Notes
Use images of at least 1200 × 630 pixels.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.