任务: 允许 CMS 用户更改将帖子分享到 Twitter 时使用的图像。Yoast 在创建 Twitter Card 时使用特色图片。
方法: 在帖子中添加自定义元字段。扩展 WPSEO_Twitter 类和/或仅扩展私有函数 output_metatag()。如果自定义元字段不为空,请使用自定义字段值而不是默认值。
代码:
if (class_exists('WPSEO_Twitter')) :
remove_action( 'wpseo_head', array( 'WPSEO_Twitter', 'get_instance' ), 40 );
add_action( 'wpseo_head', array( 'EXAMPLE_WPSEO_Twitter', 'get_instance' ), 40 );
class EXAMPLE_WPSEO_Twitter extends WPSEO_Twitter {
// etc
}
endif;
问题: remove_action 不起作用,并且 twitter 元代码被复制。插件的类和我的扩展类都在执行。
链接: https ://github.com/Yoast/wordpress-seo/blob/trunk/frontend/class-twitter.php