定义动作时,也会定义传递给回调的参数。例如,可以使用4个参数定义动作:
$a = 1;
$b = 2;
$c = 3;
$d = 4;
do_action( \'name_of_my_action\', $a, $b, $c, $d );
然后,当您钩住该操作时,您可以知道将使用多少这些参数;这些参数的值是定义操作时指定的值。例如,您可能只想使用2:
// The fourth parameter is the number of accepted
// parameters by the callback, in this case want only 2
// The parameters are passed to the callback in the same order
// they were set when the action was definied and with the same value
add_action( \'name_of_my_action\', \'my_action_callback\', 10, 2 );
function my_action_callback( $a, $b ) {
// The value of $a is 1
var_dump( $a );
// The value of $b is 2
var_dump( $b );
}
如果你看一下
init
动作,eiter输入
docs 或
source code (在WodPress 4.3.1中,它位于wp-settings.php文件的第353行),没有传递给回调的参数。
如果要将自定义参数传递给操作和筛选器回调,可以使用几个选项。I recommend this question and its answer to learn about it. 例如,使用匿名函数:
$custom = 1;
add_action( \'init\', function () use $custom {
// The value of $custom is 1
var_dump( $custom );
}, 10, 2 );