5

I am looking for a filter to define what image on a post sets the facebook OG image. In my case I have the image I want to use in a post meta field.

I have looked around but am unsure of the best way to handle this. Is there a core wordpress filter for this? If so I could not find one.

One solution that I looked into was through modifying the yoast seo plugin filters since we do have that plugin on our site. By default it looks to put the OG meta on the featured image which is not what I am looking for.

5
  • Yes, it's there: wpseo_opengraph_image. Are you definitely getting a different og:image tag? Can you try adding the filter earlier in the page load lifecycle, e.g. in a plugin not your theme? (I don't think that should be necessary though)
    – Rup
    Feb 26, 2015 at 9:46
  • "the image lives else where in the meta data" - if I'm reading it right, it'll also use metadata key _yoast_wpseo_opengraph-image - see image() in the same file - to pick the image. But I've only skimmed class-wpseo-meta very carefully, that might be the wrong name.
    – Rup
    Feb 26, 2015 at 9:51
  • Please, do not misuse moderator flags. Now you have a risen flag against yourself. Not good. Just file an edit and the system will cast a reopen vote. If the vote goes through, your question will get reopened, etc.
    – kaiser
    Feb 26, 2015 at 18:29
  • @kaiser Sorry did not know that!
    – Shawn
    Feb 26, 2015 at 19:50
  • @Rup I added it to functions.php but also into a mu plugin function. It is not working.
    – Shawn
    Feb 27, 2015 at 17:15

1 Answer 1

3

The wpseo_opengraph_image filter can only be used to modify the existing og:image. Otherwise, you will need to hook into the wpseo_opengraph action to add a different image.

Here's an example of adding an image from a custom field on the post object (assuming you're using ACF here).

function my_wpseo_opengraph() {
    global $post;

    if (isset($post)) {
        $og_image = get_field('your_field_name', $post->ID);
        if ($og_image) {
            $image_url = $og_image['sizes']['large'];
            $GLOBALS['wpseo_og']->image_output($image_url);
        }
    }
}

add_action('wpseo_opengraph', 'my_wpseo_opengraph', 29);
4
  • 1
    It looks like you can grab it using the get_post_meta() if you have it in a basic custom field and don't have ACF, as long as you are setting the global wpseo_og parameter?
    – Shawn
    Feb 11, 2016 at 16:14
  • 3
    Yep! $GLOBALS['wpseo_og']->image_output($image_url); is the only important part... The rest was just for context.
    – smilly92
    Feb 12, 2016 at 1:25
  • Thanks a lot @smilly92 Jul 2, 2020 at 12:46
  • 1
    This is no longer working. wpseo_opengraph is deprecated.
    – Jules
    May 27, 2021 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.