我的wordpress开发中有一个非常奇怪的问题,
在fucntions.php中我有以下代码
//mytheme/functions.php
$arg = "HELP ME";
add_action('admin_menu', 'my_function', 10, 1);
do_action('admin_menu',$arg );
function my_function($arg)
{
echo "the var is:".$arg."<br>";
}
输出
the var is:HELP ME
the var is:
为什么功能重复2次?为什么“帮助我”的论点被正确传递并且第二次没有通过?
我一直在努力工作2天,并在很多地方寻找解决方案,但我没有运气。
我想做的很简单!我只想使用add_action传递参数到函数?
答案 0 :(得分:0)
在“my_function”里面(虽然它是你的:)),写一行:
print_r(debug_backtrace());
http://php.net/manual/en/function.debug-backtrace.php
它将帮助您了解正在发生的事情。
或者,您可以使用XDebug(在开发服务器上)。
答案 1 :(得分:0)
首先,在你的my_function()函数中,你不是定义 $ arg。你试图回应那些不存在的东西 - 所以当它返回时,它是空的。所以你需要定义它。 (编辑添加:你尝试在函数外部定义它 - 但要使里面的函数识别它,你必须全局化参数。)
function my_function($arg) {
if(!$arg) $arg = 'some value';
echo "the var is:".$arg."<br>";
}
当你添加add时,你需要定义$ arg值:
add_action('admin_menu', 'my_function', 10, 'my value');
答案 2 :(得分:0)
您是否尝试在add_action之前添加函数?
答案 3 :(得分:0)
使用这样的匿名函数:
function my_function($arg) {
echo "the var is: $arg<br>";
}
$arg = "HELP ME";
add_action('admin_menu', function() { global $arg; my_function($arg); }, 10);
有关详细信息,请参阅this answer。