Create hooks based on an array of hook names?

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…

Leave a Comment