Wordpress:使用add_filter时如何返回值?

时间:2012-12-10 08:28:36

标签: 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, $topic_id );
}

在上面的过滤器中,有2个参数。当我add_filter时,我应该返回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;
}

2 个答案:

答案 0 :(得分:18)

这是绝对正确的。

注册过滤器(或基本上调用apply_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' );

根据上面的代码,content to be filtered将首先传递给my_filtering_function1。此函数仅接收content to be filtered而不是其他参数。

然后,内容将被传递(在被my_filtering_function1处理后)到my_filtering_function2。该函数再次只接收第一个参数。

最后,内容将传递给my_filtering_function3函数(因为它已被前两个函数更改)。这次函数将传递2个参数(因为我们指定了这个),但它不会得到argument 3参数。


apply_filters() in source

答案 1 :(得分:0)

实践add_filterapply_filters

首先要了解的是apply_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";

要了解的第五件事是您可以将其他参数传递给过滤器:

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!?";