簡體   English   中英

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函數

    \n add_filter('get_the_excerpt',array($ this,'cmtt_disable_parsing'),100);\n
  2. 我編寫了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM