我可以通过 add_action 将参数传递给我的函数吗? [英] can I pass arguments to my function through add_action?
问题描述
我可以这样做吗?将参数传递给我的函数?我已经研究了 add_action doc 但不知道如何去做.传递两个参数的确切语法是什么样的.特别是如何传递 text &整数参数.
function recent_post_by_author($author,$number_of_posts) {一些命令;}add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')
更新
在我看来,它是通过 do_action 以某种方式完成的,但是怎么做?:-)
我可以这样做吗?将参数传递给我的函数?
是的,你可以!诀窍在于您传递给 add_action 的函数类型以及您对 do_action.
- ‘my_function_name’
- 数组(实例,'instance_function_name')
- 'StaticClassName::a_function_on_static_class'
- 匿名
- 拉姆达
- 关闭
我们可以通过闭包来做到这一点一>.
//钩子的自定义参数$args = 数组 ('作者' =>6,//id'posts_per_page'=>1,//最大帖子数);//订阅带有自定义参数的钩子add_action('thesis_hook_before_post',函数()使用( $args ){最近_post_by_author( $args );});//在某处触发钩子do_action('thesis_hook_before_post');//按作者呈现帖子图块列表函数最近_post_by_author($args){//与默认参数合并$args = wp_parse_args( $args, 数组 ('作者' =>-1,'orderby' =>'发布日期','订单' =>'ASC','posts_per_page'=>25));//拉取用户的帖子$user_posts = get_posts( $args );//一些命令echo '';foreach ( $user_posts as $post ) {echo "<li>$post->post_title</li>";}回声'</ul>';}
<小时>
这是一个闭包工作的简化示例
$total = array();add_action('count_em_dude', function() use (&$total) { $total[] = count($total); } );do_action ('count_em_dude');do_action ('count_em_dude');do_action ('count_em_dude');do_action ('count_em_dude');do_action ('count_em_dude');do_action ('count_em_dude');do_action ('count_em_dude');回声内爆( ', ', $total );//0, 1, 2, 3, 4, 5, 6
<小时>
匿名与封闭
add_action ('custom_action', function(){ echo 'anonymous functions work without args!'; } );//add_action ('custom_action', function($a, $b, $c, $d){ echo '匿名函数有效,但默认参数 num 为 1,其余为空 - '; var_dump(array($a,$b,$c,$d)); } );//一种add_action ('custom_action', function($a, $b, $c, $d){ echo '如果您在优先级之后指定参数数量,匿名函数将起作用 - '; var_dump(array($a,$b,$c,$d)); }, 10, 4 );//A B C D//关闭$值 = 12345;add_action ('custom_action', function($a, $b, $c, $d) use ($value) { echo '闭包允许你包含值 - '; var_dump(array($a,$b,$c,$d, $value)); }, 10, 4 );//a,b,c,d, 12345//做吧!do_action('custom_action', 'aa', 'bb', 'cc', 'dd');
<小时>
代理函数类
class ProxyFunc {公共 $args = null;公共 $func = null;公共 $location = null;公共 $func_args = null;函数 __construct($func, $args, $location='after', $action='', $priority = 10, $accepted_args = 1) {$this->func = $func;$this->args = is_array($args) ?$args : 数组($args);$this->location = $location;如果(!空($动作)){//(可选)在构造函数中传递动作以自动订阅add_action($action, $this, $priority, $accepted_args );}}函数 __invoke() {//传递给调用的当前参数$this->func_args = func_get_args();//存储参数的位置开关($this->location){案例之后":$args = array_merge($this->func_args, $this->args);休息;案例之前":$args = array_merge($this->args, $this->func_args);休息;案例替换":$args = $this->args;休息;案例参考"://只传递对这个对象的引用$args = array($this);休息;默认://忽略存储的参数$args = $this->func_args;}//触发回调call_user_func_array( $this->func, $args );//清除当前参数$this->func_args = null;}}
示例用法 #1
$proxyFunc = new ProxyFunc(功能() {echo "";打印_r(func_get_args());wp_die();},数组(1,2,3),'之后');add_action('TestProxyFunc', $proxyFunc);do_action('TestProxyFunc', 'Hello World', 'Goodbye');//你好世界, 1, 2, 3
示例用法 #2
$proxyFunc = new ProxyFunc(功能() {echo "";打印_r(func_get_args());wp_die();},//回调函数array(1,2,3),//存储的参数'after',//存储参数的位置'TestProxyFunc',//(可选)动作10,//(可选)优先级2//(可选)增加动作参数长度.);do_action('TestProxyFunc', 'Hello World', 'Goodbye');//你好世界,再见,1, 2, 3
can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two arguments would look like. In particular how to pass text & integer arguments.
function recent_post_by_author($author,$number_of_posts) {
some commands;
}
add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')
UPDATE
it seems to me that it is done somehow through do_action but how? :-)
can I do something like that? to pass arguments to my function?
Yes you can! The trick really is in what type of function you pass to add_action and what you expect from do_action.
- ‘my_function_name’
- array( instance, ‘instance_function_name’)
- ‘StaticClassName::a_function_on_static_class'
- anonymous
- lambda
- closure
We can do it with a closure.
// custom args for hook
$args = array (
'author' => 6, // id
'posts_per_page'=> 1, // max posts
);
// subscribe to the hook w/custom args
add_action('thesis_hook_before_post',
function() use ( $args ) {
recent_post_by_author( $args ); });
// trigger the hook somewhere
do_action( 'thesis_hook_before_post' );
// renders a list of post tiles by author
function recent_post_by_author( $args ) {
// merge w/default args
$args = wp_parse_args( $args, array (
'author' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page'=> 25
));
// pull the user's posts
$user_posts = get_posts( $args );
// some commands
echo '<ul>';
foreach ( $user_posts as $post ) {
echo "<li>$post->post_title</li>";
}
echo '</ul>';
}
Here is a simplified example of a closure working
$total = array();
add_action('count_em_dude', function() use (&$total) { $total[] = count($total); } );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
do_action ('count_em_dude' );
echo implode ( ', ', $total ); // 0, 1, 2, 3, 4, 5, 6
Anonymous vs. Closure
add_action ('custom_action', function(){ echo 'anonymous functions work without args!'; } ); //
add_action ('custom_action', function($a, $b, $c, $d){ echo 'anonymous functions work but default args num is 1, the rest are null - '; var_dump(array($a,$b,$c,$d)); } ); // a
add_action ('custom_action', function($a, $b, $c, $d){ echo 'anonymous functions work if you specify number of args after priority - '; var_dump(array($a,$b,$c,$d)); }, 10, 4 ); // a,b,c,d
// CLOSURE
$value = 12345;
add_action ('custom_action', function($a, $b, $c, $d) use ($value) { echo 'closures allow you to include values - '; var_dump(array($a,$b,$c,$d, $value)); }, 10, 4 ); // a,b,c,d, 12345
// DO IT!
do_action( 'custom_action', 'aa', 'bb', 'cc', 'dd' );
Proxy Function Class
class ProxyFunc {
public $args = null;
public $func = null;
public $location = null;
public $func_args = null;
function __construct($func, $args, $location='after', $action='', $priority = 10, $accepted_args = 1) {
$this->func = $func;
$this->args = is_array($args) ? $args : array($args);
$this->location = $location;
if( ! empty($action) ){
// (optional) pass action in constructor to automatically subscribe
add_action($action, $this, $priority, $accepted_args );
}
}
function __invoke() {
// current arguments passed to invoke
$this->func_args = func_get_args();
// position of stored arguments
switch($this->location){
case 'after':
$args = array_merge($this->func_args, $this->args );
break;
case 'before':
$args = array_merge($this->args, $this->func_args );
break;
case 'replace':
$args = $this->args;
break;
case 'reference':
// only pass reference to this object
$args = array($this);
break;
default:
// ignore stored args
$args = $this->func_args;
}
// trigger the callback
call_user_func_array( $this->func, $args );
// clear current args
$this->func_args = null;
}
}
Example Usage #1
$proxyFunc = new ProxyFunc(
function() {
echo "<pre>"; print_r( func_get_args() ); wp_die();
},
array(1,2,3), 'after'
);
add_action('TestProxyFunc', $proxyFunc );
do_action('TestProxyFunc', 'Hello World', 'Goodbye'); // Hello World, 1, 2, 3
Example Usage #2
$proxyFunc = new ProxyFunc(
function() {
echo "<pre>"; print_r( func_get_args() ); wp_die();
}, // callback function
array(1,2,3), // stored args
'after', // position of stored args
'TestProxyFunc', // (optional) action
10, // (optional) priority
2 // (optional) increase the action args length.
);
do_action('TestProxyFunc', 'Hello World', 'Goodbye'); // Hello World, Goodbye, 1, 2, 3
这篇关于我可以通过 add_action 将参数传递给我的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!