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 want to know if it is a good practice according to WordPress theme or plugin development.

add_action('init','all_my_hooks');

function all_my_hooks(){

// some initialization stuff here and then

add_action('admin_init',-----);
add_action('admin_menu',----);

// more like so

}

thanks

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

In general: Yes, wait for a dedicated hook to start your own code. Never just throw an object instance into the global namespace. But init is rarely necessary.

You hook in as late as possible. If your first code runs on wp_head do not use an earlier hook. You can even cascade hooks:

add_action( 'wp_head', 'first_callback' );

function first_callback()
{
    // do something
    // then
    add_action( 'wp_footer', 'second_callback' );
}

Regarding the init hook: Use wp_loaded instead. That runs after init and after ms_site_check() was called. This way you avoid to run your plugin on an invalid sub site in a multi-site installation. Everything else is the same.

share|improve this answer
1  
+1 for wp_loaded and MS info. –  kaiser Oct 9 '12 at 14:51
    
thanks a lot for your response, still a doubt, which is better load all other hooks inside wp_loaded or load them separately ? i wonder if i add hooks in wp_loaded they will be hooked earlier instead of getting hooked after admin_init or admin_menu ? –  atinder Oct 9 '12 at 18:35
    
cascading hooks isn't an issue ? –  atinder Oct 9 '12 at 19:50
    
No, why should it be? Call the second hook only if the first was useful. –  toscho Oct 9 '12 at 19:56
add comment

I don't see the big benefits of this practice, for these reasons:

Your callback functions aren't called when registering

The add_action and add_filter functions only add an entry to global variable $wp_filter which holds all filters and actions. See source. It doesn't call your function. Your code will run only when the do_action and apply_filters are called (with appropriate hook name), which happens very late in the place where those hooks should be.

You might say that doing so will make the global variable $wp_filter getting bigger => more memory required. But I think creating a new function has the same problem.

Organizing code

Putting everything in one function force you to remember all hooks in every files in your theme/plugin. You wouldn't do something like this:

  • in header.php: add hooks and callback functions for things happen in header (like menu, registering script)
  • in content.php: add hooks and callback functions for filtering content
  • admin-menu.php: add hooks and callback functions to add admin menu

(assume that those files are put in your theme/plugin)

Instead of that, you have to:

  • put only callback functions in header.php, content.php, admin-menu.php
  • and put all hooks in a separated function in another file

=> That will make you hard to know what happens when you look at the content of header.php file. You have to search to know when these callbacks are fired.

And think about situation when you have multiple classes in your theme/plugin. Do you put all hooks of all classes in one place? Or does each class has a wrapper function that holds all hooks? It's too redundant!

Above these reason, I think it's personal style :). I see some frameworks like Hybrid does what you said. Sometimes it makes me hard to digg in those frameworks!

share|improve this answer
add comment

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.