• Resolved besweeet

    (@besweeet)


    I’m using the following functions.php snippet:

    function custom_field_to_og_image( $image ) {
    	global $post;
    
    	if ( is_singular( 'post' ) ) {
    		$image_sizes = get_field( 'customogimage', $post->ID );
    		$image       = $image_sizes['sizes']['large'];
    	}
    
    	return $image;
    }
    
    add_filter( 'wpseo_opengraph_image', 'custom_field_to_og_image' );

    When the custom field for a post is empty, it uses the featured image that is currently set for the post (expected behavior).

    If the custom field is set, only the first character of the field is used. For example, I temporarily entered “Test” into the plain-text custom field. It only appears as though only the first character in the field is captured and added after https://:

    <meta property="og:image" content="https://T" />

    If I enter a full URL into the field (including https://), the output is then:

    <meta property="og:image" content="https://h" />

    How to proceed?

    (I’m surprised Yoast SEO doesn’t have a simple, integrated way of allowing you to override the og:image…)

    • This topic was modified 3 years, 11 months ago by besweeet.
    • This topic was modified 3 years, 11 months ago by besweeet.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @besweeet,

    It’s correct that the featured image will be used as the Open Graph image by default. However, you don’t need to add custom code to override this setting. You can select the Open Graph image for any post by going to the Social tab in the Yoast SEO metabox and selecting the desired image as the “Facebook Image”. See the guide here: Why doesn’t Facebook display an image?.

    Does setting the Facebook Image in Yoast SEO work on your site? If not, is this why you’re adding a custom field and custom code? If you can give us more details or links to screenshots that show the problem, that will help us investigate.

    Thread Starter besweeet

    (@besweeet)

    Hi @priscillamc. I didn’t try adjusting the Facebook image as I assumed it was specific for Facebook. Upon further research, it looks like Facebook also relies on og:image, so going to the social tab does the trick. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can’t set custom og:image using custom field and functions.php edit’ is closed to new replies.