0

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
7
  • 1
    "pass all the args" - 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 "call do_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?
    – Sally CJ
    Commented Jul 18, 2021 at 0:06
  • @SallyCJ Question updated with sample
    – shenkwen
    Commented Jul 19, 2021 at 11:38
  • add_action('action','f1',10,3); - did you mistakenly used f1 and not f2? 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 means f1() would only be called once, i.e. via the action added by developer1.
    – Sally CJ
    Commented Jul 20, 2021 at 3:06
  • But even if that's a mistake, developer2 actually made another mistake on his do_action() syntax - he supplied just 2 maximum args ($a - TRUE, and $b) to the hook callbacks, but earlier on he actually called add_action() while expecting to receive 3 args via the hook. So as stated in the current answer, the sample scenario wasn't actually a do_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.
    – Sally CJ
    Commented Jul 20, 2021 at 3:33
  • @SallyCJ The number of arguments is not the concern here. I intentionally made the example like this but it seems this has drifted the question away. The concern is with the first parameter. Developer1 expects it to be false while Developer expects it to be true is the point I am trying to make. Are these expectations legit in their own logic?
    – shenkwen
    Commented Jul 20, 2021 at 11:47

1 Answer 1

0

In the described scenario, f1 was added and it was indicated it only takes a single parameter, so only one parameter is provided ( the first ). So f1 is never called with more than one parameter.

However, it is possible to make a mistake, if you call add_action and declare that f1 can receive 2 parameters, then it will be called with 2 parameters, even if only 1 is possible, generating a PHP warning.

The same is true if you pass one parameter, f2 would generate a PHP warning because it expects more than one parameter but none are provided. Default arguments can help here.

However, this is the other developers problem for using add_action incorrectly. This isn't something you should be concerned about as long as you are documenting your actions and are consistent in how you trigger them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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