• Resolved Daniel Chase

    (@riseofweb)


    I am adding a featured image via the PHP function set_post_thumbnail().

    Doing so does not update Yoast’s og:image content.

    Is there a way programmatically I can update this as well? For example through update_post_meta()?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Michael Ti?a

    (@mikes41720)

    Hello @riseofweb

    Setting a featured image doesn’t necessarily guarantee it will also output as the og:image tag in the Yoast SEO plugin. If you would like to add the necessary metadata, we made changes starting from v14.0 of Yoast SEO for WordPress on how to do so here — https://developer.yoast.com/blog/yoast-seo-14-0-adding-metadata/

    Thread Starter Daniel Chase

    (@riseofweb)

    Thank you for your response.

    Ideally I would like to tell the Yoast Plugin that the post has just been updated and for it to run whatever functions it does when a post is updated not to filter what is outputted. If I go to the edit screen on the post after doing set_post_thumbnail() and just click on update, it does set the featured image as the og:image.

    Not ideal, but I chose this “band-aid” work around to use. FYI, All of my featured images are a set size, so I don’t need to worry about the image dimensions.

    function hotswap_og_img($str) {
    	if ( is_singular('post') ) {
    		$image = get_the_post_thumbnail_url($post->ID, 'full');
    		if (!empty($image)) {
    			return $image;
    		}
        }
    }
    add_filter('wpseo_opengraph_image', 'hotswap_og_img');

    Based on this https://github.com/Yoast/wordpress-seo/issues/1060#issuecomment-481759769

    Plugin Support Md Mazedul Islam Khan

    (@mazedulislamkhan)

    Thanks for sharing the relevant code snippet with us. We understand you need to update the relevant page after changing the featured image before you can see the relevant featured image into the Yoast SEO generated open graph meta. To make this automated, you’ll need to use the Yoast SEO v14.0 API given above using custom implementation.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Updating the featured image in PHP’ is closed to new replies.