Give us a second

Remove an Action Hook Added with a Class in WordPress

Remove an Action Hook Added with a Class in WordPress

So if you don’t know what an action hook is or how to remove one in the first place, then this isn’t a post for you.

Anyhoo, so as you know a function is normally hooked like this:

add_action('init','some_function');

or

 add_action('init','some_function', 10); 

And to remove it you’d simply use the remove_action function making sure to match the priority argument if there is one, like so:

 remove_action('init','some_function', 10); 

Simples, but you already know this. The problem is when a theme or plugin hooks a function via a class like so:

class Some_Class {
 function __construct(){
  add_action('init', array($this, 'class_function'));
 }
 function class_function(){
  //some code
 }
}
//instantiate class
$some_class = new Some_Class;

To unhook the function now you need to first get access to the class object and then use it like so:

//get the class object
global $some_class;

remove_action('init', array($some_class,'class_function'));

Remember, if there is a priority argument to make sure that you match it like we did earlier.
That’s it! Good luck.

Photo by Luca Bravo on Unsplash

By |2018-09-26T22:03:34+00:00July 13th, 2018|Tech|

About the Author:

I'm a full-stack developer and graphic designer. When I'm not doing those things I'm dabbling in video and music production, and when I'm spent from all that I'm probably watching anime or something educational... or funny.
By using this website, you are agreeing to our use of cookies to enhance your user experience. Ok