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(){

some where you do something like:

add_action('hook_name', 'some_hook');

and then some where in the theme you call:


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:


class AisisCore_Hooks{

    protected $_hooks;

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



    public function init(){}

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


I then tried doing:

$array = array(

$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(); ?


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 this:

  public function hook_name()

The hook creation pattern that you are using...

public function hook_name(){

some where you do something like:

add_action('hook_name', 'some_hook');

... is something that I remember doing some time ago, and many or most of the tutorials online suggest that. However, if you grep the source for a hook that does not have a (known) corresponding function like the_content which is a hook and a template tag...

grep -Rni 'pre_get_posts' *

... and then grep again for an associated function...

grep -Rni 'function pre_get_posts' *

... you won't find it for many hooks. The Core does not use that public function hook_name(){ do_action('hook_name'); } pattern in every case, and it doesn't seem to be used to actually create the hook. It appears that do_action or apply_filters does all the work. The example on the Codex page for do_action confirms this. That example uses a hook called i_am_hook but there is no function of that name, only do_action( 'i_am_hook', $a, $b );. You can prove this by doing something very simple like:

function test_new_action($a,$b) {
$a = 'abc';
$b = 'def';

There are a couple of more recent tutorials that I found, though I am sure there are more, demonstrating the same thing:

You don't need to create those functions at all and you don't need to fire that 'empty' hook. You just need to use do_action or apply_filters where you need to run the hook.

You only need to create those function hook_name(){ do_action('hook_name'); } functions if and only if you want to run the hook by calling it by name the way that the Core does with the wp_head and wp_footer hooks.

So, I don't think you need to be doing this at all for most hooks and for the ones that you might want to call by name, you aren't creating the functions that would allow you to do that. The only way that I have been able to make that work, and I don't like it, is with eval-- very rough proof of concept:

$funcnames = array(
foreach ($funcnames as $f) {
  $str = 'function '.$f.' () { do_action("'.$f.'"); }';
function test_new_action_v2() {
function test_new_action_v3() {

I think that that is the only way to do it but that has some speed penalties, and will probably have scope issues, especially given that you are executing it inside a class (but I haven't tested that). See: https://stackoverflow.com/questions/7337883/php-define-functions-with-variable-names

Hopefully that will give you enough information to evaluate what you need to do and do not need to do, and what you can do, to make this work for your purposes.


It seems, you're mixing up Hooks (i.e., Actions and Filters) and Shortcodes.

» Shortcodes — Perform tasks anywhere you want

Suppose you want to print a particular text somewhere on your website.
In order to do this, first create a function inside your functions.php, then add an individual shortcode for this very function, and finally use it anywhere you want. If you want to do so for pages/posts, just use the shortcode itself (e.g., [my_shortcode]), while you can use the do_shortcode('my_shortcode') function anywhere within the template files of your theme or within a function defined in your functions.php.

Here is an example of how to use a shortcode:

function wpse85485_shortcode() {
    return "That's my new shortcode. <strong>Yay!</strong>";
add_shortcode('my_shortcode', 'wpse85485_shortcode');

» Actions — Perform tasks triggered by a particular action

Suppose you want to bring to life Casper the Friendly Ghost. :)
Also suppose you'd want Casper to spook any visitor no matter what page he/she may visit.
First, you set up the function (to spook the visitors), and then hook it to a certain appropriate action.

Here is an example of how to use an action hook:

function casper_the_friendly_ghost() {
    echo "<script>alert('Boo!');</script>";
add_action('wp_footer', 'casper_the_friendly_ghost');


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.