I have this idea where I would create a class that would take in a set of hook names then create them, for me to use when ever, and where ever.

Currently the way to create a hook is we do something like:

function some_hook_name(){

And then from there we write some other function, well call - add_content and then pass it to the action as such:

add_action('some_hook_name', 'add_content');

and there we go.

But what if I wanted to create a class where I do something like:

$array_of_hook_names = array(
    'hook_one', 'hook_two', ...

so that later on, I could do:

add_action('hook_one', 'add_content');

Is there any way to do this? Could I just do something like:

public function setup_hooks($array){
    foreach($array as $hook_name){


1 Answer 1


Your proposal is OK - you can see this in action even in the WordPress itself. See admin-ajax.php where you can find this piece of code:

// Register core Ajax calls.
if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get ) )
    add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );

if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $core_actions_post ) )
    add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );

add_action( 'wp_ajax_nopriv_autosave', 'wp_ajax_nopriv_autosave', 1 );

if ( is_user_logged_in() )
    do_action( 'wp_ajax_' . $_REQUEST['action'] ); // Authenticated actions
    do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ); // Non-admin actions

As you can see, the action hooks are generated dynamicaly...

  • Based on what I see here, they already have the partial name, so my idea could work.
    – TheWebs
    Feb 11, 2013 at 21:48

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.