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!
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!
Share Improve this question asked Jun 24, 2019 at 20:56 Jody HeavenerJody Heavener 1853 silver badges13 bronze badges 2 |1 Answer
Reset to default 0I think you are headed down the right path, but I think that func_get_args()
is only going to give you the arguments passed to that function. That is how I have always used it in the past.
I took a different approach that I think might work. I found debug_backtrace()
to be helpful in debugging Actions and Filters before. And the 4th element in that array it returns you is the do_action call with all the arguments that live inside of the call you are looking to make.
Here is a snippet I put together with a few actions I tested:
<?php
// Here are the Actions I tested against.
$events = array(
'profile_personal_options',
'profile_update',
'show_user_profile',
'show_user_profile'
);
foreach ($events as $event) {
add_action($event, function($args = null) {
$backtrace = debug_backtrace();
// This element is going to be the do_action call
echo '<pre>';
print_r($backtrace[3]['function']);
echo '</pre>';
$action_arguments = $backtrace[3]['args'];
// The first element of this is going to be the $event
array_shift($action_arguments);
// Leaving you with the rest of the parameters available to that action
echo '<pre>';
print_r($action_arguments);
echo '</pre>';
}, 100);
}
If this is not what you are looking for, or I am off...let me know and I'd be happy to take my answer down!!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745373381a4624894.html
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. – Jacob Peattie Commented Jun 25, 2019 at 10:09