Retrieve a value from Yoast SEO to use to set a default twitter card image honoring overrides

So Yoast SEO has an issue where you can't define a global default twitter card image.

So I'm looking to both set a default twitter card image but allow for the override on the specific pages to actually work.

I'm currently using:

$default_opengraph = 'https://website.com/image.png';
function default_opengraph() {
    global $default_opengraph; return $default_opengraph;
}
add_filter('wpseo_twitter_image','default_opengraph');

Which allows me to globally set the default twitter card image, but the problem with this is:

  1. It prevents page specific overrides from working (Yoast SEO Customs / Featured Image)
  2. It needs to be manually defined.

Anyone able to help find a fix for this, it's something YoastSEO itself should have in the plugin and they have loads of requests on WordPress forums, github as well as on their site asking for this and they've been asking for several years but they still haven't added it...

Hopefully someone can help with this as it's largely applicable and useful for many many users.

Thanks

Topic plugin-wp-seo-yoast functions filters twitter seo Wordpress

Category Web


Here's how I fixed it. If you paste this code in your theme's functions.php, it will use the image set for Facebook also for Twitter if no image is set for Twitter.

If there is not image set for both Facebook and Twitter, it falls back to the default Facebook image in the Yoast settings (unfortunately, you can't provide a default Twitter image there, so we'll be using the Facebook one).

  // Add Twitter image to every page /article if none is defined

add_filter('wpseo_twitter_image', 'change_twitter_image');

function change_twitter_image($image) {

    if (!$image) {

        global $post;
        
        if (!$image = get_post_meta($post->ID)["_yoast_wpseo_opengraph-image"][0]) {
            $image = get_option("wpseo_social")["og_default_image"];
        }

    }
 
    return $image; 
  
}; 

If there is already an image set then it is passed to the wpseo_twitter_image filter when it's called as a parameter:

/**
 * Filter: 'wpseo_twitter_image' - Allow changing the Twitter Card image.
 *
 * @api string $img Image URL string.
 */
$img = apply_filters( 'wpseo_twitter_image', $img );

so you can easily fix your filter so that it won't overwrite an existing value, e.g.

$default_opengraph = 'https://website.com/image.png';
function default_opengraph( $img ) {
    if ( is_string( $img ) && ( $img !== '' ) ) {
        // We have a value set
        return $img;
    }

    // No image: return the default
    global $default_opengraph;
    return $default_opengraph;
}
add_filter( 'wpseo_twitter_image','default_opengraph' );

However at first glance there is a default image option, og_default_image, which is serialised into the wp_options value wpseo_social. I don't know if / where you can configure this in the admin site though.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.