add_filter()给一个过滤器钩子添加一个回调方法

目录

描述

译文

将函数连接到特定过滤器动作。

过滤器是WordPress发布的一种hook,在不同类别的文本被添加到书库据或发送到浏览器窗口前对这些文本进行修改。插件可用过滤器API规定在修改特定类型的文本时,执行一个还是多个PHP函数。在Plugin API上查阅过滤器hook列表。

原文

Hook a function to a specific filter action.

add_filter() 描述

用法

 <?php add_filter$tag$function_to_add$priority$accepted_args ); ?> 

add_filter() 用法

参数

$tag

(string) (必填) The name of the existing Filter to Hook the $function_to_add argument to. You can find a list of Filter Hooks here.

默认值: None

$function_to_add

(callback) (必填) The name of the function to be called when the custom Filter is applied.

默认值: None

$priority

(integer) (可选) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the filter.

默认值: 10

$accepted_args

(integer) (可选) The number of arguments the function(s) accept(s). In WordPress 1.5.1 and newer hooked functions can take extra arguments that are set when the matching apply_filters() call is run.

默认值: 1

add_filter() 参数

注意

  • Although you can pass the number of $accepted_args, you can only manipulate the $value. The other arguments are only to provide context, and their values cannot be changed by the 过滤器 function.
  • You can also pass a class method as a callback.

Static class method:

Instance method:

  • You can also pass an an anonymous function as a callback. For example:

Anonymous functions were introduced in PHP 5.3.0. Check Hosting WordPress requirements and double check your PHP version before using them.

If your version of PHP is older than 5.3.0, you can use create_function() instead. But keep in mind that lambda functions created by create_function() are not cached by APC or any other optimizer. So don't use create_function() if callback is supposed to be used more than few times or it has complex logic.

add_filter() 注意

历史

  • 添加于 版本: 0.71

add_filter() 历史

源文件

add_filter() 函数的代码位于 wp-includes/plugin.php.

add_filter() 源文件

相关