Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.77k stars 892 forks source link

Fallback OG image Twitter Image unexpected results #12996

Open carasmo opened 5 years ago

carasmo commented 5 years ago

Please give us a description of what happened.

Expected results:

  1. I have a fallback OG image in the Yoast settings.
  2. When I don't have a featured image on that page, then both twitter and og get the url.

Strange results:

  1. I have a fallback OG image in the Yoast settings.
  2. If there is a featured image BUT it's not displayed on the page (used for archives) then the OG image is the fallback but the Twitter image is the featured image assigned but not displayed.

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.

carasmo commented 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; 

} 
Djennez commented 5 years ago

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.