1
votes

I want to customize the behavior of CM tooltip plugin. 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. When the wpseo_opengraph_desc gets activated it reanables parsing. The actual parsing happens in cmtt_glossary_parse function.

I wrote a new plugin and tried the following:

  1. I wrote my cmtt_disable_parsing function with higher precedence

    add_filter('get_the_excerpt', array($this, 'cmtt_disable_parsing'), 100);
    
  2. I wrote my cmtt_glossary_parse function which checks the conditions and then calls the CMTooltipClossaryFrontent::cmtt_glossary_parse function

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

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? Is it a bad practice to instantiate a plugin class and call its methods somehwere else, lets say inside another plugin?

1
I think this is much more than you need, but maybe will help quite a bit: How to remove a filter that is an anonymous object? - brasofilo

1 Answers

2
votes

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.

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.

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.

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