Custom og:image

#621171
  • Resolved Anonymous
    Rank Math free

    Hello, I am trying to set up a custom featured image (og:image) for every post. I am trying to set up the first image in the post, to achieve this I created this code snippet:

    add_filter("rank_math/opengraph/facebook/image", "set_og_image_as_first_image_in_aside");

    function set_og_image_as_first_image_in_aside($attachment_url) {
    global $post;

    // Check if it's a single post and has a featured image set
    if (is_single() && has_post_thumbnail($post->ID)) {
    $thumbnail_id = get_post_thumbnail_id($post->ID);
    $thumbnail_image = wp_get_attachment_image_src($thumbnail_id, 'full');

    if ($thumbnail_image) {
    // Get the first image within the <aside> structure
    preg_match('/<div class="afxshop-image">\s*<img src="(.*?)"/', $attachment_url, $matches);

    if (isset($matches[1])) {
    $first_image_url = $matches[1];

    // Use the first image URL as the OpenGraph image
    return esc_url($first_image_url);
    }
    }
    }

    // If no changes, return the original attachment URL
    return $attachment_url;
    }

    I had used the filter of RankMath, as you can see. However, it does not work.

    Could you please provide me some clarification in case that I am doing wrong.

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thanks for contacting us and sorry for the inconvenience caused.

    The filter will return the default og:image URL if there is no featured image set for the post and if there is a featured image set then it will return the first image of the post as the og:image.

    Can you please confirm if you have a featured image set for the page you have shared?

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Hello Jaideep, thanks for your response.

    I dont have a featured image for the post that I shared.

    I only have a default image on the OpenGraph Thumbnail configurations.

    What I am trying to achieve is set as a featured image the first image within an <aside> structure with a specific class (<div class=”afxshop-image”>).

    Let me know if I need change the code.

    Thanks

    Hello,

    Yes, you need to remove the check if the featured image exists on the page before performing the actions you would like to run.

    As we mentioned above you are only running the code when the post has a featured image, so that check in the condition should be removed entirely, otherwise, it won’t run on posts such as the one you shared.

    Also, please note that this filter won’t add the image as a featured image, it will add it as the OpenGraph image property.

    Don’t hesitate to get in touch if you have any other questions.

    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 ‘Custom og:image’ is closed to new replies.