0

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

  • I don't know this code at all, but at first glance it will use a value og_default_image (read both in the twitter code and the opengraph-image base class) and I think this value is serialised into wp_options in wpseo_social. – Rup Feb 6 at 23:14
0

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.