Wordpress override CM Tooltip plugin function - php

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:
I wrote my cmtt_disable_parsing function with higher precedence
add_filter('get_the_excerpt', array($this, 'cmtt_disable_parsing'), 100);
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?

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

Related

override a function wrapped with if (!function_exists)

I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way

What is the cleanest way to override a Wordpress plugins' hook instantiated with $this

I am trying to modify a Wordpress plugins' method without changing the plugin core code itself. I need to set some status flag in a different part of the website.
The method is defined in the plugin class like so:
add_action('plugins_action_name', [$this, 'local_method_name']);
The plugin method does not offer any actions or filters to hook into.
If this wasn't Wordpress but plain PHP I would write my own class, which extends the plugins' original class but has it's own method by the same name - but in Wordpress this gets me nowhere since I just move the problem from one plugin file to the next.
Is there a way to remove the plugins action altogether? And then redefine my own version of it?
Since the add_action has been called with $this - how would I remove it in some functions.php file?
Thanks
UPDATE
Indeed I want to learn how to handle different scenarios. Not expecting a one size fits all solution, but I do need to know how to avoid core changes, as each core-change complicates my deploy/update process exponentially.
Sure I will add some more details. My example plugin is WooCommerce Germanized Pro which comes with a WC_GZDP_Download_Handler::download() method which I needed to modify, in order to set some sort of "Warehouse downloaded the invoice" flag for each order. Which is later used to highlight new orders, filter orders that have been handed over to fulfilment etc.
There seems to be a Invoice Helper class which does in fact have a singleton pattern.
my modification in the download class is trivial:
public static function download( $pdf, $force = false ) {
... /* original code above, now my modifications */
if ($check_some_codition) {
$invoice = $pdf->get_invoice();
update_post_meta($invoice->get_id(), 'some-flag', 'some-value');
}
/* then resume original function (return the file to the browser) */
self::out( $filename, $file, $force );
}

How to use hooks in child theme to override a plugin?

I want to add some HTML code to a plugin but I want to do that in a child theme, the function that I wanna override it is in a file named as admin-functions.php
I want to add extra buttons to booked_render_custom_fields function
admin-functions.php:
function booked_render_custom_fields($calendar = false) { ?>
<button class="button">Text</button>;
<?php }
any suggestion? thanks
you can't really override a function like that. you can only use the filters and hooks that the plugin implements. but you got some options:
a) make a copy of the plugin, change it's name and make the changes you need. after that, it wont get updates anymore. you'll have to implement them manually.
b) you could add just one line to the original plugin and define a filter or hook to which you subscribe in your functions.php . so you only have to fix this one line after updating the plugin.
c) since the newer versions of wordpress, you have the possibilites to run your own code after any shortcode. so you could add or inject your own html into the output of the shortcode. https://developer.wordpress.org/reference/hooks/do_shortcode_tag/

WordPress hooks for executing right before any action or page loading

I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?
Last hook before loading the template is template_redirect
You can use it like this:
function my_function(){
// your code goes here
}
add_action( "template_redirect", "my_function" );
You can use init hook. It will be performed before element or HTML code. It's also useful to manage POST and GET variables. The syntax is something like this:
function yourfunction() {
dosomething();
}
add_action('init', 'yourfunction');
A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference
Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
Other hooks must be suggested and will be added in a future release if is a good suggestion.
Or you'd have to edit the core files ;)
You mean a hook when all wordpress function will available but before any output including headers sent?
Well hook your function on init. That will call when visiting site. If you want this hook only for admin area then it is admin_init.

Can I override a PHP Function that is called in another file/plugin?

First of, I have read "Override default php function" and namespaces doesn't fulfil my need. I have also looked into PHP.net's override_function() but that can't help me with my problem either.
I am using a Wordpress plugin called Jigoshop as an eCommerce solution but in some cases I cannot remove the actions I need to apply my own structure to a 'single product' page. I do not want to edit the plugin files themselves as a plugin update may negate and remove my previous changes. Essentially, I want to control the output through my /themes/mytheme/functions.php file.
Has anyone come across this before whereby the original function is contained in a file I do not want to edit for that same 'updating' reason?
Thanks
EDIT (2012-11-21):
I have a custom function in my functions.php file like so:
function prepare_jigoshop_wrappers() {
remove_action('jigoshop_before_main_content', 'jigoshop_breadcrumb', 20);
add_action('jigoshop_before_main_content', 'custom_jigoshop_breadcrumb', 10);
}
add_action('wp_head', 'prepare_jigoshop_wrappers');
This essentially allows me to apply my own structure & configuration. For other default functions, it is a little more difficult. For example, the 'quantity selector', 'Add to Cart' button and 'stock availability' all are contained within a function called jigoshop_template_single_summary in the jigoshop_template_actions.php file calling the likes of _title, _price, _excerpt, _meta, _sharing & _add_to_cart.
The order these are displayed, I cannot seem to change. Therefore, I want to essentially redefine function jigoshop_template_single_summary() {...}
You may want to look into PECL extension. It may do what you need, docs on PHP.NET.
runkit_function_redefine
(PECL runkit >= 0.7.0)
I don't know this plugin very well but probably this is what you Need Theming with Jigoshop
Override some functions in php(also in common..) is never a good idea. If the plugin doesn't provide a way to hook into it's functionality is sometimes better to use an alternative plugin or rewrite the core code..

Categories