Failed to invoke other hook from the init hook

Related to this question (Use wp init hook to call other hooks?) but not the same.

Sometimes, I found that the hook will failed to run when I place inside the init hook, e.g.

Not Work:

add_action('init','my_init');
function my_init() {
    add_filter('locale', ...
}

Work:

add_action('init','my_init');
add_filter('locale', ...

Of course some hooks/filters will just work okay inside the init hook, but definitely not all of them.

So it seems to me that it is a bad practice to chain actions/filters together?

2 Answers
2

The locale hook happens long before init. To see when which hook, variable, constant, function, class or file is available install my plugin T5 WP Load Order.

You get a looong file with a very detailed log. Search for Hook: locale, then for Hook: init. You will see that you need plugins_loaded as parent action if you want to chain hooks.

Is it a good practice? It depends. If you need the second callback only when the first was running successful, then yes. If both callbacks should run independently of each other, then no.
Chaining should reflect the logic of your program.

Leave a Comment