WordPress过滤器之add_filter()
过滤器钩子它要做的是通过执行过滤器函数来改变对象或变量的值,就相当于对变量或者对象进行过滤或者处理。过滤器可以提高了WordPress的灵活性,方便自定义开发。无论是制作WordPress主题还是WordPress插件,我们都会使用add_filter()添加过滤器来实现一些特殊需求。下面就详细的讲一下WordPress过滤器之add_filter() 。
1、add_filter()函数
add_filter()定义在wp-includes/plugin.php文件中,add_filter() 函数的源代码如下:
1 | function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
2、add_filter()函数用法
1 | add_filter( string $tag, callable $function_to_add, int $priority = 10,int $accepted_args = 1 ) |
$tag:必填(字符串)。挂载回调函数的过滤器名称。
$function_to_add:必填(可调用的函数)。过滤器应用时调用的回调函数。
$priority:可选(整型)。用于规定函数被执行的顺序,函数与特定动作关联。较小的数字匹配较早的执行,同等优先级的函数按加入action的顺序被执行。
$accepted_args:可选(整型)。add_filter() 过滤器可接受的参数个数,默认为1。
3、add_filter()函数实例
1 | //定义过滤器函数custom_title,把所有的标题title修改为不言不语 |