I'm building a plugin that iterates across and parses the arguments of various WordPress hooks, and because of the flexibility needed I'm passing a function as the callable parameter in add_action
:
foreach ($events as $event) {
add_action($event, function() use($event) {
$args = func_get_args();
// Do something with $event and $args
}, 100);
}
The problem I'm experiencing is that this only ever seems to retrieve the first argument passed to the action.
Take profile_update
, for example. It should pass the User ID, as well as an object of old user data, but if I inspect I only get the User ID:
var_dump($args)
// array(1) { [0]=> int(3) }
This isn't limited to that specific hook, either; I can't seem to get more than the first argument for any hook set up this way. Any suggestions are appreciated!
add_action()
only the number of arguments that you specify with the 4th argument ofadd_action()
are passed to your function. By default this is 1. If your callback accepts 3 arguments you need to specifyadd_action( 'action_name', 'callback', 10, 3 );
. So if you set this number to the maximum number accepted by any of the hooks you're hooked to, you might get somewhere.