If you are a WordPress developer, then you must have definitely used action and filter hooks. You might have also known that add_action()
hook can be removed using remove_action()
& add_filter()
hook can be removed using remove_filter()
functions. Well, that’s super easy, isn’t it? But it’s not going to be straight forward if you want to remove action/filter hook added within class. Not to worry, this tutorial explains how to remove action or filter hook added within a class.
Before we start talking about this topic, a quick description of what is add_action
and add_filter
hooks are in WordPress.
add_action is a hook that can be used to launch at specific points during execution or when an event occurs. A general format of add_action() function looks as below:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
add_filter is a hook that can be used to modify something when an event occurs. The general format of add_filter() function looks as below:
add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
With that quick definition, we will see an example action & filter hook. Let us say there’s a plugin that registers an action and filter as below:
add_action( 'wp_head', 'tg_generator_head'); add_filter( 'get_pages', 'tg_filter_get_pages');
Now, to remove these action & filter hooks, we just need to use remove_action() and remove_filter() functions as shown below:
remove_action( 'wp_head', 'tg_generator_head'); remove_filter( 'get_pages', 'tg_filter_get_pages');
As I said before, this is super easy. But the catch is to remove the action and filter that’s added by a plugin within a class. Let us say, the same action hook that we saw above is added within a class as shown below:
class TG_Knowledge_Base { //Constructor function __construct(){ add_action( 'wp_head', 'tg_generator_head'); } function tg_generator_head(){ echo apply_filters('tg_generator_head', '<meta name="generator" content="Techglimpse Theme v' . TG_VERSION_NUMBER. '" />' . "\n" ); } } $tg_knowledge_base_init = new TG_Knowledge_Base(); //instance of a class
Basically, the above code adds a meta generator tag to the website source. But if you are a regular reader by my tutorials, then you might know that I’m not a big fan of meta generator tags and I tend to remove those. Well, that’s what I’ll do now, but for an action that’s added within a class “TG_Knowledge_Base
“.
Remove Action/Filter Hook Added Within Class
Since the action is registered within a class, we need to use the class object to access its hook. So let’s do that.
global $tg_knowledge_base_init; remove_action('wp_head', array($tg_knowledge_base_init, 'tg_generator_head'));
What if an action is added within a static class that does not need a class instance for accessing? Then use the class name instead of an instance as shown below:
remove_action('wp_head', array('TG_Knowledge_Base', 'tg_generator_head'));
Love it? Let me know your comments.