2017-10-10 99 views
0

任务: 允许CMS用户更改将帖子分享到Twitter时使用的图像。 Yoast在创建Twitter卡片时使用精选图片。如何在WordPress主题中正确地扩展Yoast的WPSEO_Twitter类

该方法: 为帖子添加一个自定义元字段。扩展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

回答

0

我认为这将是更容易的过滤器添加到wpseo_twitter_image改变形象,你的愿望。

东西沿着

add_filter("wpseo_twitter_image", function($img) { 
    if($myimg = get_post_meta(get_the_ID(), "custom-twitter-image", true)) { 
     return $myimg; 
    } 
    return $img; 
}); 

线或许应该为你工作,如果我理解正确的你。