apply_filters(string $ tag, mixed $ value )
调用添加到过滤器钩子的功能。
说明
通过调用此函数调用附加到过滤钩$ tag的回调函数。该函数可以用于通过使用$ tag参数指定的新钩子的名称调用此函数来创建新的过滤器钩子。
该函数允许添加其他参数并传递给钩子。
// Our filter callback function
function example_callback( $string, $arg1, $arg2 ) {
// (maybe) modify $string
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 3 );
/*
* Apply the filters by calling the 'example_callback' function we
* "hooked" to 'example_filter' using the add_filter() function above.
* - 'example_filter' is the filter hook $tag
* - 'filter me' is the value being filtered
* - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
参数
$tag
(字符串)(必须)过滤器的名字。
默认值:None
$value
(混合)(必须)要过滤的值,如果没人过滤则直接返回这个值。
$var
(混合) (可选)传给过滤函数额外的变量参数,辅助过滤函数对返回值进行操作,可以添加无限个。
返回值
(混合)过滤后的值,如果没人过滤则直接返回 $value 的值。
例子
没人过滤:
echo apply_filters( 'test', '可以被修改的值' );
//打印结果:
可以被修改的值
有人过滤:
function test_func(){
return '修改值';
}
add_filter( 'test', 'test_func' );
echo apply_filters( 'test', '可以被修改的值' );
//打印结果:
修改值
接收参数:
function test_func(){
return '修改值';
}
add_filter( 'test', 'test_func' );
function test_func2( $text ){
return $text . '2';
}
add_filter( 'test', 'test_func2' );
echo apply_filters( 'test', '可以被修改的值' );
多个参数:
function test_func( $text, $var, $var2 ){
return '修改值' . $var1 . $var2;
}
add_action( 'test', 'test_func', 10, 3 );
echo apply_filters( 'test', '可以被修改的值', '辅助值1', '辅助值2' );
其它
此函数位于:wp-includes/plugin.php

