Create hooks based on an array of hook names?

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:

Read More
function some_hook_name(){
    do_action('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){
        do_action($hook_name);
    }
}

Ideas?

Related posts

Leave a Reply

1 comment

  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
    else
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ); // Non-admin actions
    

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