fbpx

How To Remove an Action Hook or Filter Added By a Class in WordPress

Here is a quick WordPress developer tip.

WordPress is great for allowing developers to extend plugins and themes using action hooks and filters.

Sometimes we need to remove an already declared action or filter in a plugin or theme.

It’s pretty easy to do this in functional programming using remove_action() and remove_filter().

Here are some examples:

add_filter( 'the_content', 'my_content_filter_callback_function' );
add_action( 'admin_init', 'my_admin_hook_callback_function' );

Removing these types of actions and filters is easy:

remove_filter( 'the_content', 'my_content_filter_callback_function' );
remove_action( 'admin_init', 'my_admin_hook_callback_function' );

Removing actions and filters gets a bit more complex when they are declared inside a PHP Class.

Let us look at a simple plugin class that registers an action hook:

class Blue_Widgets {
 
	function __construct() {
		add_action( 'widgets_init', array( $this, 'register_widget_callback' ) );
        }
 
        function register_widget_callback(){
         register_widget( 'Blue Widget' );
        }
 
}
 
$blue_widgets = new Blue_Widgets();

The action hook (and filters) are registered in an array that binds to the current class using $this.

How do you replicate that to remove the hook or filter?

Removing Action Hooks and Filters In a Class

When removing action hooks and filters, remember to use the exact same creation parameters.

For the example above, you need to use the class object to remove the hook.

There are two ways to achieve this.

Using the Class Object Instance

The last line highlighted in the code above shows the class object instance saved to the variable $blue_widgets.

For defined objects in a variable, you can use it to remove the hook or filter.  Example:

// Get the variable containing the previously declared class object instance
global $blue_widget;

remove_action( 'widgets_init', array( $blue_widget, 'register_widget_callback' ) );

When dealing with static classes with no instantiation, you need to use the following method.

Removing Hooks and Filters When a Class Does Not Have an Instance

In this case, we need to use the class name itself to remove hooks and filters.

remove_action( 'widgets_init', array( 'Blue_Widgets', 'register_widget_callback' ) );

Matching the Priority and Arguments Parameters

The functions add_action() and add_filter() have an additional two optional parameters int $priority = 10, int $accepted_args = 1.

These parameters determine the priority (defaults to 10) and the number of accepted arguments (defaults to 1).

When removing an action hook or filter, you must also match the priority parameter.

Let’s look at the previous code block with those two parameters added to see how we can remove it later.

class Blue_Widgets {
 
	function __construct() {
		add_action( 'widgets_init', array( $this, 'register_widget_callback' ), 12, 'true' );
        }
 
        function register_widget_callback( $debug ){
         register_widget( 'Blue Widget' );
        }
 
}
 
$blue_widgets = new Blue_Widgets();

In the example above,  line 4 has been modified to add a priority of “12” and a function parameter “true”.

To remove the action, match you must also include the matching priority parameter.

// Get the variable containing the previously declared class object instance
global $blue_widget;

remove_action( 'widgets_init', array( $blue_widget, 'register_widget_callback' ), 12 );

Hopefully, you find this developer tip useful in your plugins and themes.

Keep In Touch

Wil

Wil is a dad, consultant, developer, conference organiser, speaker and business mentor. He co-organizes the WordPress Sydney meetup group and has been on the orgnising committee for WordCamp Sydney since 2014. He speaks at many technical events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.
If You Enjoyed This Post
Join Our Newsletter
Give it a try, you can unsubscribe anytime.
Close