Is there a filter to define the OG image on a given post?

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);

Leave a Comment