Jen*_*nny 9 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 );
}
Run Code Online (Sandbox Code Playgroud)
在上面的过滤器中,有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;
}
Run Code Online (Sandbox Code Playgroud)
Nik*_*lov 19
那是绝对正确的.
注册过滤器(或基本上调用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' );
Run Code Online (Sandbox Code Playgroud)
鉴于上面的代码,content to be filtered将首先传递给my_filtering_function1.此函数仅接收content to be filtered而不是其他参数.
然后内容将被传递(在被处理之后my_filtering_function1)到my_filtering_function2.该函数再次只接收第一个参数.
最后,内容将被传递给my_filtering_function3函数(因为它已被前两个函数更改).这次函数将传递2个参数(因为我们指定了这个),但它不会得到argument 3参数.
Den*_*rov 12
add_filter和apply_filters首先要知道的是apply_filters返回它的第二个参数,试试这个functions.php:
echo apply_filters('cat_story', 'A cat'); // echoes "A cat"
Run Code Online (Sandbox Code Playgroud)
第二件事要知道的是,在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"
Run Code Online (Sandbox Code Playgroud)
第三件事要知道的是我们可以添加多个过滤器:
// #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"
Run Code Online (Sandbox Code Playgroud)
第四件事要知道的是,您可以按特定顺序应用过滤器:
// #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";
Run Code Online (Sandbox Code Playgroud)
第五件事要知道的是,您可以将其他参数传递给过滤器:
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!?";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12181 次 |
| 最近记录: |