Dynamic Open Graph Images

#985796
  • Resolved HotScup
    Rank Math free

    Hello, I was using All in One SEO previously and switched to RankMath today, Previously I was getting the post images in link preview for the articles I was sharing in Telegram, but after switching to RankMath, I am not able to see the Image in Link Preview.

    Note: I need to set it dynamically for posts as I am running a news site and the news are getting inserted automatically (without any human interection) so I can’t go with manual image setting.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello, we apologize for the issue you are facing with the Open Graph images not displaying in link previews after switching to Rank Math.

    To set dynamic Open Graph images for your posts, Rank Math uses a hierarchy to determine which image to display. Here’s how it works:

    1. OpenGraph image (set in the Rank Math Meta Box)
    2. Featured image (set for the post)
    3. First image in the post
    4. Default OpenGraph thumbnail (set globally)

    Since you mentioned that your news articles are inserted automatically, you can ensure that the first image in each post is being recognized by Rank Math as the Open Graph image. Here are a few steps to check:

    1. Ensure Featured Images Are Set: If your posts have featured images, make sure they are being set correctly during the automatic insertion process. This will allow Rank Math to use them as Open Graph images.

    2. Check Image Dimensions and Format: Make sure that the images being used meet the recommended dimensions of 1200px X 630px and are in supported formats (jpeg, png, gif, WebP). If the images do not meet these criteria, they may not display correctly in link previews.

    3. Set Default OpenGraph Thumbnail: If there are cases where no featured image or first image is available, you can set a default OpenGraph thumbnail by navigating to Rank Math → Titles & Meta → Global → OpenGraph Thumbnail in your WordPress dashboard. Upload an image here that meets the recommended specifications.

    4. Use Filters for Customization: If you want to override the first image in the post with a specific default OpenGraph thumbnail, you can use a filter in your theme’s functions.php file:

      add_filter('rank_math/opengraph/pre_set_content_image', function() {
         return true;
      });

    For more detailed guidance, you can refer to our documentation on Open Graph/Social Preview Image.

    If you continue to experience issues, please reply to this ticket with your site URL so we can assist you further.

    Sources:
    Open Graph/Social Preview Image Is Not Displayed » Rank Math
    Open Graph/Social Preview Image Is Not Displayed » Rank Math
    Open Graph Meta Tags » Rank Math

    HotScup
    Rank Math free

    Hey, I can’t add featured image for each posts as I need to download the image in wordpress server which is not fesible at all for me. is there any other option?

    Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    Can you please share some affected URLs here so that we can check the issue further for you?

    However, please note that Rank Math selects an image for the og:image property based on the following priority.

    1. Rank Math looks in the Social tab if there’s any image set for the OpenGraph image. If so, It ignores the fallback/default OpenGraph image, featured image or the image in the post content.

    2. If there’s no image set in the social media/social tab, Rank Math looks for the featured image set in your post and ignores rest of other settings as mentioned in #1.

    3. If no featured image is found, Rank Math looks into the post content. If there’s any image is found, Rank Math ignores the fallback image in settings.

    4. Lastly, if Rank Math doesn’t find any featured image or images in post content, it sets the image from the settings default OpenGraph image (if you’ve set any).

    Now, if you want to set the first image of your post content as OpenGraph image (og:image) you can the following filter to your website:

    /**
     * This following code snippet prioritizes 'first image' in post content, even if there's any image set in default OpenGraph image and featured image.
     * In the beginning, It looks for the "first image" in the post content.
     * If not found, this will skip it's part and the featured image will be set for 'og:image' property.
     * If there's no featured image, the 'default OpenGraph image' from Rank Math settings will be applied.
     **/
    
    function get_first_image() {
      global $post, $posts;
      $first_image = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
      $first_image = $matches[1][0];
    
      if(empty($first_image)) {
        /** Currently doing nothing
         * 
         * You may try customizing the code here to set a default image if there's no image available inside post content.
         * Or, you may leave it as it is. 
         **/
      }
      return $first_image;
    }
    
    add_filter( "rank_math/opengraph/facebook/image", function( $attachment_url ) {
    	global $post;
    	
    	if (get_the_post_thumbnail_url($post->ID)) {
    		$attachment_url = get_first_image();
    	}
    	
    	return $attachment_url;
    });
    
    add_filter( "rank_math/opengraph/twitter/image", function( $attachment_url ) {
    	global $post;
    	
    	if (get_the_post_thumbnail_url($post->ID)) {
    		$attachment_url = get_first_image();
    	}
    	
    	return $attachment_url;
    });
    

    Here’s how you can add filter/hook to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Please be advised that, you can still override the code above by adding image in the Social media/Social tab for a specific post. That gives you the flexibility to set custom image for a specific post. In short, if you set any image in the social tab, that will be set for og:image property, even if the filter is active and will work for other posts as well.

    Let us know how it goes. Looking forward to helping you.

    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.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Dynamic Open Graph Images’ is closed to new replies.