gpt4 book ai didi

php - 我可以通过 add_action 将参数传递给我的函数吗?

转载 作者:IT王子 更新时间:2023-10-29 00:56:36 24 4
gpt4 key购买 nike

我可以这样做吗?将参数传递给我的函数?我已经研究过add_action doc但不知道该怎么做。传递两个参数的确切语法是什么样的。特别是如何传递文本和整数参数

function recent_post_by_author($author,$number_of_posts) {
some commands;
}
add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')

更新

在我看来,这是通过do_action 以某种方式完成的。但如何? :-)

最佳答案

can I do something like that? to pass arguments to my function?

是的,你可以!诀窍在于你传递给 add_action 的函数类型。以及您对 do_action 的期望.

  • ‘我的函数名’
  • array( 实例, 'instance_function_name')
  • ‘StaticClassName::a_function_on_static_class’
  • 匿名
  • lambda
  • 关闭

我们可以用 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>';
}

这是闭包工作的简化示例

$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

匿名与封闭

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' );

代理函数类

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;
}
}

示例用法#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

示例用法#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

关于php - 我可以通过 add_action 将参数传递给我的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2843356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com