简体   繁体   English

Wordpress覆盖CM Tooltip插件功能

[英]Wordpress override CM Tooltip plugin function

I want to customize the behavior of CM tooltip plugin. 我想自定义CM tooltip插件的行为。 From what I see in the code the plugin is a class that has the following filters, which are kind of self descriptive. 根据我在代码中看到的,该插件是一个具有以下过滤器的类,这些过滤器是自我描述的。

    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);
}

I want to enable/disable the parsing functionality according to my needs ( post type etc). 我想根据自己的需要启用/禁用解析功能(帖子类型等)。

The plugin code has get_the_excerpt filter, that checks some conditions and disables the parsing. 插件代码具有get_the_excerpt过滤器,该过滤器检查某些条件并禁用解析。 When the wpseo_opengraph_desc gets activated it reanables parsing. wpseo_opengraph_desc被激活时,它可重新解析。 The actual parsing happens in cmtt_glossary_parse function. 实际解析在cmtt_glossary_parse函数中进行。

I wrote a new plugin and tried the following: 我编写了一个新插件,并尝试了以下操作:

  1. I wrote my cmtt_disable_parsing function with higher precedence 我以较高的优先级编写了cmtt_disable_parsing函数

    \nadd_filter('get_the_excerpt', array($this, 'cmtt_disable_parsing'), 100); add_filter('get_the_excerpt',array($ this,'cmtt_disable_parsing'),100);\n
  2. I wrote my cmtt_glossary_parse function which checks the conditions and then calls the CMTooltipClossaryFrontent::cmtt_glossary_parse function 我编写了cmtt_glossary_parse函数,该函数检查条件,然后调用CMTooltipClossaryFrontent :: cmtt_glossary_parse函数

    \n add_filter('the_content', array($this, 'cmtt_glossary_parse'), 10008); add_filter('the_content',array($ this,'cmtt_glossary_parse'),10008); \n 

but none of them works. 但它们都不起作用。 Also, when i instantiate the original plugin inside my plugin, the original plugin is not working properly ( it does not parse the content) 另外,当我实例化插件中的原始插件时,原始插件无法正常工作(它无法解析内容)

Any help would be appreciated on how to customize properly the plugin functionality. 任何有关如何正确自定义插件功能的帮助将不胜感激。 Should i create a new plugin or is it better to put the code in functions.php? 我应该创建一个新的插件还是将代码放入functions.php中更好? Is it a bad practice to instantiate a plugin class and call its methods somehwere else, lets say inside another plugin? 实例化一个插件类并以其他方式调用其方法是否不好,比如说在另一个插件内部?

Finally i found a working solution for me. 终于我找到了一个可行的解决方案。 So i drop a line here in case someone else finds it useful. 因此,我在这里下一行,以防其他人觉得有用。

I read this https://iandunn.name/the-right-way-to-customize-a-wordpress-plugin/ guide, which describes the alternatives someone has, if he wants to override a plugin functionality. 我阅读了这份https://iandunn.name/the-right-way-to-customize-a-wordpress-plugin/指南,该指南描述了某人如果要覆盖插件功能时可以使用的替代方法。

In my case the solution was similar as the one described in the "Override their callbacks" section. 在我的情况下,解决方案与“覆盖其回调”一节中描述的解决方案相似。 I downloaded his example that overrides google-authenticator plugin and followed pretty much the same tactic. 我下载了他的示例,该示例重写了google-authenticator插件,并且遵循了几乎相同的策略。

Especially for the cm tooltp plugin that i wanted to customize removing the original hooks and re-adding them if my requirements are met worked for me. 特别是对于我想要自定义的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);

and register my callback that enables again the original plugin functions if my requirements are met using the following code 并使用以下代码注册满足我的要求的回调,该回调可再次启用原始插件功能

//if (my_condition)
add_filter('the_content', array(CMTooltipGlossaryFrontend::$calledClassName, 'cmtt_glossary_parse'), 9999);
.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM