Open carasmo opened 5 years ago
This is my slapped together work around.
add_filter( 'wpseo_opengraph_image', 'filter_wpseo_opengraph_image', 10, 1 );
/**
* Opengraph Social Image from Featured Image
* If user does not enter a social image AND the page has a featured image, use that
* otherwise use the default assigned in settings.
* Won't work unless a default image is set via the settings in Yoast.
*/
function filter_wpseo_opengraph_image( $img ) {
global $post;
if( isset( $post ) && has_post_thumbnail() ) :
$og = get_post_meta( $post->ID, '_yoast_wpseo_opengraph-image', true );
if ( empty( $og ) ) :
$img = get_the_post_thumbnail_url( $post->ID, 'facebook-instagram' );
else:
$img = $og;
endif;
endif;
return $img;
}
add_filter( 'wpseo_twitter_image', 'filter_wpseo_twitter_image', 10, 1 );
/**
* Twitter Social Image from Featured Image
* If user does not enter a social image AND the page has a featured image, use that
* otherwise use the default assigned in settings.
*/
function filter_wpseo_twitter_image( $img ) {
global $post;
if( isset( $post ) && has_post_thumbnail() ) :
$tw = get_post_meta( $post->ID, '_yoast_wpseo_twitter-image', true );
if ( empty( $tw ) ) :
$img = get_the_post_thumbnail_url( $post->ID, 'facebook-instagram' );
else:
$img = $tw;
endif;
endif;
return $img;
}
Hi @carasmo, thanks for your report and analysis. The Twitter and Facebook meta images are calculated via different systems and I agree that this causes strange behavior. I'll ask our product team to determine if this is something that we want to change so that they behave more consistently.
Please give us a description of what happened.
Expected results:
Strange results:
What I would expect is that both the OG and the Twitter image get the featured image whether or not it's displayed on the page.
Please describe what you expected to happen and why.
See above.
How can we reproduce this behavior?
See first.
Technical info
Latest all. Not using Gutenberg. N/A.