Passing function into add_action always returns the first argument

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!

Read more here:: Passing function into add_action always returns the first argument

Leave a Reply

Your email address will not be published. Required fields are marked *