Why do some hooks not work inside class context?

Sometimes certain hooks need to be fired at certain times. Example, some hooks need to be fired upon init.

Add this to your __construct()

add_action('init', array(&$this, 'init'));

Then add this function, which will contain all hooks that need to be fired upon init.

public function init(){
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
}

Another Example:

add_action( 'init', function () {

    add_action( 'hook_name', 'function_name', 10, 3 );
    add_action( 'hook_name', __NAMESPACE__ . '\namespaced_function_name', 10 );
    add_action( 'hook_name', '\specific\namespace\function_name', 5 );

}, 1 );

You will want to read about the hooks and when they are fired. So you know when and where to trigger your actions. Plugin API/Action Reference

Leave a Comment