I'm trying to understand the hook system. According to wordpress codex, do_action
executes all hooked functions. So
do_action('hook_name',$arg1,$arg2,$arg3)
will actually pass all the args to all of the hooked functions right? And I think different plugin developers can all hook their function to a certain action. So when one of the developers call do_action
with the args that are meant for his function, these args are actually passed to other functions that were written by other developer, won't this potentially cause problem?
For a very simple example
function f1($a){
if ($a) {
die();
}
...
}
function f2($a,$b,$c){
if (!$a){
die();
}
....
}
//developer1
add_action('action','f1',10,1);
// develper2
add_action('action','f1',10,3);
do_action('action',TRUE,$b); //developer2 call action
do_action()
would pass all args, but only if requested. But yes, all the hook args are made available to the hook callbacks, i.e. they can receive all the args if explicitly requested (e.g.add_action( 'foo', 'bar_func', 10, 3 )
). As for the "unexpected results" and "calldo_action
with the args that are meant for his function", can you further explain that and demonstrate a sample unexpected result that came across your mind?add_action('action','f1',10,3);
- did you mistakenly usedf1
and notf2
? Because note that WordPress would not add the second action because it uses the same callback and priority, despite the number of accepted args is different than the first action. So that meansf1()
would only be called once, i.e. via the action added by developer1.do_action()
syntax - he supplied just 2 maximum args ($a
-TRUE
, and$b
) to the hook callbacks, but earlier on he actually calledadd_action()
while expecting to receive 3 args via the hook. So as stated in the current answer, the sample scenario wasn't actually ado_action()
problem; instead, it's the developers who needed to know what they're doing - e.g. as for developer1, he must study what the hook does or is for, what its parameters and default callback(s) hooked onto the hook, if any.