Is it possible to create an action hook using do_action() within add_action()?

Is it possible to create a new action hook like do_action('my-hook-name'); inside a call to add_action();?

I’m aware this code doesn’t work, but I was thinking something along the lines of…
add_action('init', do_action('my-hook-name'));

Read More

or

Is the only way to actually add a callback function to init, then inside my callback function create my hook?

add_action('init', 'my_callback');
function my_callback(){
    do_action('my-hook-name');
}

Related posts

1 comment

  1. Creating an answer based upon responses via comments on original question of: Is it possible to create an action hook using do_action() within add_action()?

    Yes it is possible to create an action hook using do_action() on a call to add_action().

    For clarification, the following code does NOT work:

    add_action('init', do_action('my-hook-name'));

    As stated by @IvanHanák in the comments to the original question asked; It is possible to create an action hook using do_action() on a call to add_action() by using anonymous functions.

    An example of creating an action hook using do_action() on a call to add_action() using anonymous functions:

    add_action('some-existing-hook-name', function(){do_action('my-new-hook-name');});

    However, it should be noted that using anonymous functions can make it difficult to debug or remove hooks.

Comments are closed.