Can’t set custom og:image using custom field and functions.php edit
-
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…)
- The topic ‘Can’t set custom og:image using custom field and functions.php edit’ is closed to new replies.