Hooks are not executing

Based on my understanding of hooks, you create a hook, by doing do_action(‘hook_name’); then add something to said hook and call the method where you want it to do the hook, so:

public function hook_name(){
    do_action('hook_name');
}

some where you do something like:

add_action('hook_name', 'some_hook');

and then some where in the theme you call:

hook_name();

and there ya go, where you call that function your hook will execute. Well I attempted to create a class to simplify this, because what if you have a theme with say 50,000 hooks (dont ask why, I am going with the extreme), thats 50,000 functions much like the one I did above.

So based on: This previous post I attempted to create a class such as:

<?php

class AisisCore_Hooks{

    protected $_hooks;

    public function __construct($hooks){
        $this->_hooks = $hooks;

        $this->_setup_hooks($this->_hooks);

        $this->init();
    }

    public function init(){}

    protected function _setup_hooks($hooks){
        foreach($hooks as $hook=>$param){
            if(is_array($param)){
                do_action($hook, implode(',', $param));
            }else{
                do_action($hook);
            }
        }
    }

}

I then tried doing:

$array = array(
    'hook_name_one'
);

$hooks = new AisisCore_Hooks($array);

function test(){
    echo "I am a test for this idea";
}

add_action('hook_name_one', 'test');

But soon realized that I am doing an action which has nothing attached to it. So my question is:

Do I, in my class, Want to pass in a action name and a function and do add action and then out side call do_action(); ?

so:

protected function _setup_hooks($hooks){
    foreach($hooks as $tag=>$action){
        add_action($tag, $action);
    }
}

then do:

do_action(//what ever the tag is);

3 Answers
3

The do_action is in the wrong place. It is calling it’s self.

You have this:

   public function hook_name()
    {
     do_action('hook_name');
    }

Do this:

  do_action('hook_name');
  public function hook_name()
   {
   do_action('other_hook_name');
   }

Leave a Comment