admin管理员组文章数量:1327102
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 = '.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:
- It prevents page specific overrides from working (Yoast SEO Customs / Featured Image)
- 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
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/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:
- It prevents page specific overrides from working (Yoast SEO Customs / Featured Image)
- 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
Share Improve this question edited Feb 6, 2020 at 17:22 Ryflex asked Feb 6, 2020 at 15:53 RyflexRyflex 831 silver badge11 bronze badges 1- 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 Commented Feb 6, 2020 at 23:14
2 Answers
Reset to default 2Here'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/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.
版权声明:本文标题:functions - Retrieve a value from Yoast SEO to use to set a default twitter card image honoring overrides 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742205137a2432674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论