Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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...

share|improve this answer
    
Based on what I see here, they already have the partial name, so my idea could work. –  Kyle Adams Feb 11 '13 at 21:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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