Meks
Do you know you can try any of our premium WordPress themes for free?Show me the themes
Remove an action or a filter hook added within a class in WordPress

Remove an action or a filter hook added within a class in WordPress

24Shares

As many of you probably know, WordPress has an ability to remove actions and filters added via add_action() and add_filter() functions. This can be easily done with a simple call of remove_action() or remove_filter() function. Here is a quick usage example:

Let’s say that there is a plugin registering an action and a filter:

And you want to remove them via your theme (or a plugin), for any reason. You just use this:

That was easy, right? But the real problem may appear if action is registered in a plugin within its own class. Take a look at the example below:

Using remove_action() if action hook is added within a class

Since action is registered within a class, you must use the class object in order to access its hooks. In the example below, there is an elegant way to remove the action hook.

Using remove_action() if class doesn’t have an instance

If a class doesn’t have an instance for any reason (i.e. a static class), use its name instead of an instance:

Bonus tip: priorities must match

Interesting thing is that if a priority parameter is used while adding an action, remove_action() call must have the same priority argument in order to remove the action properly. Take a look:

So, in this case, you should use the same “57” priority parameter on action removal:

That’s it developers! Hope this one was helpful so feel free to leave us your thoughts in the comment section below.

24Shares
Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

5 comments

Leave a Reply

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

You don't need to have loads of money to get a world-class WordPress website! All our themes are rated with the highest 5-star rating. Have a look and choose the best one for you.
Meks WordPress Themes
Meks