Wordpress覆盖CM Tooltip插件功能


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函数中。

我写了一个新的插件,并尝试了以下操作:

  1. 我用更高优先级的编写了cmtt_disable_parsing函数

    add_filter('get_the_except',数组($this,'cmtt_disable_parsing'),100);
  2. 我编写了cmtt_glossary_parse函数,该函数检查条件,然后调用CMTooltipClossaryFrontent::cmtt_glossary_parce函数

    add_filter('se_content',数组($this,'cmtt_glossary_parse'),10008);

但它们都不起作用。此外,当我在插件中实例化原始插件时,原始插件无法正常工作(它无法解析内容)

如有任何关于如何正确定制插件功能的帮助,我们将不胜感激。我应该创建一个新的插件,还是把代码放在functions.php中更好?实例化一个插件类并在其他地方调用它的方法,比如在另一个插件内部,这是一种糟糕的做法吗?

我终于找到了一个可行的解决方案。所以我在这里写了一行,以防其他人觉得它有用。

我读过这个https://iandunn.name/the-right-way-to-customize-a-wordpress-plugin/指南,其中描述了如果某人想覆盖插件功能,他可以选择的选项。

在我的案例中,解决方案类似于"覆盖他们的回调"部分中描述的解决方案。我下载了他的覆盖谷歌验证器插件的例子,并遵循了几乎相同的策略。

特别是对于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);
.....