0

Here is a simple example to explain my question. 3 functions are hooked into the myaction. Each take a different number of args and the args can be a number of an array of numbers. So args can also be different data type.


add_action('myaction', 'my_multiplication', 20, 3);
add_action('myaction', 'my_addition', 30, 1);
add_action('myaction', 'my_division', 50, 2);

function my_addition ($a = array()) {
    $sum = 0;
    echo '

'; foreach ($a as $i => $val) { $sum += $val; echo ($i ? ' + ' : '') . $val; } echo ' = ' . $sum . '

'; } function my_multiplication ($a, $b, $c) { echo '

' . $a . ' * ' . $b . ' * ' . $c . ' = ' . ($a * $b * $c) . '

'; } function my_division ($a, $b) { echo '

' . $a . ' / ' . $b . ' = ' . ($a / $b) . '

'; }

How do I make a do_action call to run the 3 hooked function? Here was what I tried, which did not work. I want 2, 3, 4 be passed for multiplication, array(11, 22, 33) for addition, and 60, 20 for division.


do_action('myaction', 2, 3, 4, array(11, 22, 33), 60, 20);

Update I wonder if passing arguments can be more specific as below:


do_action(
    'myaction',
    array(
        'my_multiplication' => array(2, 3, 4),
        'my_addition' => array(array(11, 22, 33)),
        'my_division' => array(60, 20)
    )
);
0

All the additional arguments passed to do_action() are passed as arguments, in order, to any hooked callback function. The callback functions can only choose how many of them to use, by setting the $accepted_args parameter of add_action(). They can't choose which arguments to accept.

So you would need to do something like this:

add_action('myaction', 'my_multiplication', 20, 3); // Accept the first 3 arguments.
add_action('myaction', 'my_addition', 30, 4); // Accept the first 4 arguments.
add_action('myaction', 'my_division', 50, 6); // Accept the first 6 arguments.


function my_multiplication ($a, $b, $c) {
    echo '' . $a . ' * ' . $b . ' * ' . $c . ' = ' . ($a * $b * $c) . '';
}

function my_addition ($a, $b, $c, $d = array()) {
    $sum = 0;
    echo '';
    foreach ($d as $i => $val) {
        $sum += $val;
        echo ($i ? ' + ' : '') . $val; 
    }
    echo ' = ' . $sum . '';
}

function my_division ($a, $b, $c, $d = array(), $e, $f) {
    echo '' . $e . ' / ' . $f . ' = ' . ($e / $f) . '';
}

Note how my_addition() needs to accept at least 4 arguments to get access to $d, which contains the array, and my_division() needs to accept all 6 arguments to get $e and $f, the last two numbers.

2
  • Then the priorities and the argument counts passed into the 3 add_action call are not independent. If the priority order changes, the argument count needs to change as well. And the argument counts do not represent the actual number of arguments the hooked function will use. They way of pass argument in this complicated case looks awkward. I am wondering if we can assign the arguments specifically to each hooked function.
    – Ryan Yang
    Nov 5 '21 at 12:35
  • What? The way arguments passed is extremely simple. All arguments are passed, in order. That’s it. It’s harder to get simpler than that. Also, priority and accepted args have nothing to do with each other. The argument count would not need to change if the priority was changed. What gave you that idea? What problem are you trying to solve here? It seems like you may be trying to use actions for something they’re not suited for. Nov 5 '21 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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