How to remove an action added by plugin from theme?

As we all know, flexibility and customization is possible in WordPress because of action and filter. We can add as many actions and filters and remove them as well.

Lets take a look how we can remove an action from theme that is added via plugin.

Lets say, we have a class Wte_Paypal_Gateway:

class Wte_Paypal_Gateway

This class has a global object instance called $plugin_public.

global $plugin_public;

An action wte_paypal_submit_btn is hooked in a tag called wte_paypal_gateway with priority of 11. This action add a paypal submit button.

add_action( 'wte_paypal_gateway', $plugin_public, 'wte_paypal_submit_btn', 11 );

function wte_paypal_submit_btn()
$btn = '<img alt="" border="0" width="1" height="1" src="" >';
$btn .='<input type="image" name="submit" border="0"
src="" alt="PayPal - The safer, easier way to pay online">';
echo $btn;

Suppose, we want to have our own custom submit button instead of what plugin is providing then we can do that quiet easily.
First, we have to remove the action that is adding the submit button. For this, we have to use a function remove_action(). Now, a function remove_my_action() is created which uses the function remove_action() and this function is hooked into init.

add_action( 'init', 'remove_my_action' );
function remove_my_action()
global $plugin_public;
remove_action( 'wte_paypal_gateway', $plugin_public, 'wte_paypal_submit_btn', 11 );

* Note that the priority while adding the action and removing the action must be same. Here, 11 is the priority.

Finally, if we want to have our custom submit button then we hook a function into the tag wte_paypal_gateway with the same priority as the plugin submit button that is 11. Below is the sample code:

add_action( 'wte_paypal_gateway', 'add_my_action', 11 );
function add_my_action()
<input type="submit" value="Custom Submit">

So, in this way we can remove an action that is being added via plugin from theme and then we can add anything we want in the same hook.

Leave comment

Your email address will not be published. Required fields are marked with *.