[英]Wordpress override CM Tooltip plugin function
我想自定义CM tooltip
插件的行为。 根据我在代码中看到的,该插件是一个具有以下过滤器的类,这些过滤器是自我描述的。
class CMTooltipGlossaryFrontend {
/*
* FILTERS
*/
add_filter('get_the_excerpt',array(self::$calledClassName,'cmtt_disable_parsing'), 1);
add_filter('wpseo_opengraph_desc', array(self::$calledClassName, 'cmtt_reenable_parsing'), 1);
/*
* Make sure parser runs before the post or page content is outputted
*/
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_parse'), 9999);
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_createList'), 9998);
add_filter('the_content', array(self::$calledClassName, 'cmtt_glossary_addBacklink'), 10000);
}
我想根据自己的需要启用/禁用解析功能(帖子类型等)。
插件代码具有get_the_excerpt
过滤器,该过滤器检查某些条件并禁用解析。 当wpseo_opengraph_desc
被激活时,它可重新解析。 实际解析在cmtt_glossary_parse
函数中进行。
我编写了一个新插件,并尝试了以下操作:
我以较高的优先级编写了cmtt_disable_parsing函数
\n add_filter('get_the_excerpt',array($ this,'cmtt_disable_parsing'),100);\n
我编写了cmtt_glossary_parse函数,该函数检查条件,然后调用CMTooltipClossaryFrontent :: cmtt_glossary_parse函数
\n add_filter('the_content',array($ this,'cmtt_glossary_parse'),10008); \n
但它们都不起作用。 另外,当我实例化插件中的原始插件时,原始插件无法正常工作(它无法解析内容)
任何有关如何正确自定义插件功能的帮助将不胜感激。 我应该创建一个新的插件还是将代码放入functions.php中更好? 实例化一个插件类并以其他方式调用其方法是否不好,比如说在另一个插件内部?
终于我找到了一个可行的解决方案。 因此,我在这里下一行,以防其他人觉得有用。
我阅读了这份https://iandunn.name/the-right-way-to-customize-a-wordpress-plugin/指南,该指南描述了某人如果要覆盖插件功能时可以使用的替代方法。
在我的情况下,解决方案与“覆盖其回调”一节中描述的解决方案相似。 我下载了他的示例,该示例重写了google-authenticator插件,并且遵循了几乎相同的策略。
特别是对于我想要自定义的cm tooltp插件,如果满足我的要求,可以删除原始的挂钩并重新添加它们,这对我来说很有效。
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_parse'), 9999);
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_createList'), 9998);
remove_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_addBacklink'), 10000);
并使用以下代码注册满足我的要求的回调,该回调可再次启用原始插件功能
//if (my_condition)
add_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_parse'), 9999);
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.