Use wp init hook to call other hooks?

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

2

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.

Leave a Comment