本文介绍了在 wordpress 中使用 add_action 传递参数!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 wordpress 开发中有一个很奇怪的问题,
I have a very strange problem in my wordpress development,
在 fucntions.php 我有以下代码
in fucntions.php I have the following code
//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 次?为什么帮我"这个参数被正确地传递了,而它第二次没有被传递?
Why the function repeated 2 times? Why has the argument "help me" been passed correctly and the 2nd time it havent been passed?
我已经尽力了 2 天,并在很多地方搜索以找到解决方案,但我没有运气.
I have been trying all my best for 2 days and searched in many places to find a solution but I had no luck.
我想做的很简单!我只想使用 add_action 将参数传递给函数?
What I am trying to do is simple! I just want to pass argument to a function using add_action?
推荐答案
像这样使用匿名函数:
function my_function($arg) {
echo "the var is: $arg<br>";
}
$arg = "HELP ME";
add_action('admin_menu', function() { global $arg; my_function($arg); }, 10);
有关详细信息,请参阅此答案.
See this answer for details.
这篇关于在 wordpress 中使用 add_action 传递参数!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!