WordPress:使用add_过滤器时如何返回值?

WordPress:使用add_过滤器时如何返回值?,wordpress,add-filter,Wordpress,Add Filter,我已经读了很多次WordPress codex,但仍然不明白如果涉及多个参数,如何返回值。例如: function bbp_get_topic_title( $topic_id = 0 ) { $topic_id = bbp_get_topic_id( $topic_id ); $title = get_the_title( $topic_id ); return apply_filters( 'bbp_get_topic_title', $title, $top

我已经读了很多次WordPress codex,但仍然不明白如果涉及多个参数,如何返回值。例如:

function bbp_get_topic_title( $topic_id = 0 ) {
    $topic_id = bbp_get_topic_id( $topic_id );
    $title    = get_the_title( $topic_id );

    return apply_filters( 'bbp_get_topic_title', $title, $topic_id );
}
在上面的过滤器中,有2个参数。当我添加过滤器时,我应该返回2个值,还是只返回我需要的值?如果需要标题,以下示例是否正确

add_filter( 'bbp_get_topic_title', 'my_topic_title', 10, 2 );

function my_topic_title( $title, $topic_id ){
  $title = 'my_example_title';
  return $title;
}

完全正确

注册筛选器(或基本上调用
apply\u filters
)时,应使用至少两个参数调用函数-要应用的筛选器的名称和将应用筛选器的值

传递给函数的任何其他参数都将传递给筛选函数,但只有当它们请求附加参数时才传递给它们。下面是一个例子:

// Minimal usage for add_filter()
add_filter( 'my_filter', 'my_filtering_function1' );
// We added a priority for our filter - the default priority is 10
add_filter( 'my_filter', 'my_filtering_function2', 11 );
// Full usage of add_filter() - we set a priority for our function and add a number of accepted arguments.
add_filter( 'my_filter', 'my_filtering_function3', 12, 2 );

// Apply our custom filter
apply_filters( 'my_filter', 'content to be filtered', 'argument 2', 'argument 3' );
给定上述代码,要过滤的
内容将首先传递给
my\u filtering\u function1
。此函数只接收要筛选的
内容
,而不接收附加参数

然后内容将被传递(经过
myu filtering\u function1
处理后)到
myu filtering\u function2
。同样,函数将只接收第一个参数

最后,内容将被传递到
my\u filtering\u function 3
函数(因为它已被前两个函数更改)。这一次,函数将被传递2个参数(因为我们指定了这一点),但它不会得到
参数3
参数


实践
添加过滤器
应用过滤器
首先要知道的是
apply\u filters
返回其第二个参数,请在
functions.php
中尝试:

echo apply_filters('cat_story', 'A cat'); // echoes "A cat"
要知道的第二件事是,在
apply_filters
返回“A cat”之前,它在可以修改“A cat”的地方应用过滤器,使用
add_filter
添加过滤器:

function add_chasing_mice($cat) {
    return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice');

echo apply_filters('cat_story', 'A cat'); // echoes "A cat is chasing a mice"
要知道的第三件事是我们可以添加多个过滤器:

// #1
function add_chasing_mice($cat) {
    return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice');

// #2
function add_something_else($cat) {
    return $cat . ', but it\'s not gonna catch it';
}
add_filter('cat_story', 'add_something_else');

echo apply_filters('cat_story', 'A cat'); // echoes "A cat is chasing a mice but it\'s not gonna catch it"
要知道的第四件事是,您可以按特定顺序应用过滤器:

// #1
function add_chasing_mice($cat) {
    return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice', 10); // 10 - is priority

// #2
function add_something_else($cat) {
    return $cat . ', but it\'s not gonna catch it';
}
add_filter('cat_story', 'add_something_else'); // 10 as well, if omitted 

// The filter will be applied before `add_chasing_mice` and `add_something_else`
function replace_the_cat($cat) {
    return 'A dog';
}
add_filter('cat_story', 'replace_the_cat', 9); // 9 < 10, so the filter will be applied first

echo apply_filters('cat_story', 'A cat'); // echoes "A dog is chasing a mice but it's not gonna catch it";
/#1
功能添加鼠标($cat){
return$cat.“正在追逐一只老鼠”;
}
添加过滤器(“猫故事”,“添加追逐老鼠”,10);//10-优先权是什么
// #2
函数add\u something\u other($cat){
返回$cat',但它不会抓住它';
}
添加过滤器(“猫故事”,“添加其他东西”);//如果省略,也可以使用10
//过滤器将在“添加鼠标”和“添加其他内容”之前应用`
功能替换\u目录($cat){
返回“一只狗”;
}
添加过滤器(“猫故事”,“替换猫”,9);//9<10,因此将首先应用过滤器
echo应用过滤器(“猫故事”、“猫”);//呼应“一只狗在追逐一只老鼠,但它不会抓住它”;
要知道的第五件事是,您可以将其他参数传递给过滤器:

function add_chasing_mice($cat) {
    return $cat . ' is chasing mice';
}
add_filter('cat_story', 'add_chasing_mice', 10); // 10 - is priority

function add_something_else($cat, $exclam, $wft) {
    return $cat . ', but it\'s not gonna catch it' . $exclam . $wft;
}
add_filter('cat_story', 'add_something_else', 10, 3); // 3 arguments

function replace_the_cat($cat) {
    return 'A dog';
}
add_filter('cat_story', 'replace_the_cat', 9); // 9 < 10, so the filter will be applied first

echo apply_filters('cat_story', 'A cat', '!!!', '!1wTf!?'); 
// 3 arguments are: 'A cat', '!!!', '!1wTf!?'.
// echoes "A dog is chasing a mice but it's not gonna catch it!!!!1wTf!?";
功能添加鼠标($cat){
return$cat.“正在追逐老鼠”;
}
添加过滤器(“猫故事”,“添加追逐老鼠”,10);//10-优先权是什么
函数add\u something\u else($cat、$惊叹、$wft){
返回$cat',但它不会抓住它。$惊叹。$wft;
}
添加过滤器('cat_story','add_something_other',10,3);//3个论点
功能替换\u目录($cat){
返回“一只狗”;
}
添加过滤器(“猫故事”,“替换猫”,9);//9<10,因此将首先应用过滤器
echo应用过滤器('cat_story'、'A cat'、'!!!、'!1wTf!?');
//3个参数是:'猫','!!!','!1wTf!?”。
//呼应“一只狗正在追赶一只老鼠,但它不会抓住它!!!!1wTf!?”;

如果我返回自定义的$topic\u id,会发生什么?你不能:)我的意思是,你可以执行
返回$topic\u id
而不是
返回$title-但这只会更改最初筛选的值,不会影响
$topic\u id
变量。您可以尝试这样定义您的函数:
function my\u filtering\u function($title,&$topic\u id)
-这样
$topic\u id
将作为引用传递,但我不能100%确定它是否有效。很好的解释!我把它添加到WordPress文档中;