WordPress函数文档add_filter()

给一个过滤器钩子添加一个回调方法 描述 译文 将函数连接到特定过滤器动作。 过滤器是WordPress发布的一…

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

描述

译文

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

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

原文

Hook a function to a specific filter action.

  • Glossary: Filters

用法

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

参数

$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

注意

  • Hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the 动作 ‘comment_id_not_found’ will pass the comment ID to each callback.
  • 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.

历史

  • 添加于 版本: 0.71

源文件

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

相关

Filters:
has_filter(),
add_filter(),
apply_filters(),
current_filter(),
merge_filters(),
remove_filter(),
remove_all_filters()

  • 原文:http://codex.wordpress.org/Function_Reference/add_filter
类别:WordPress函数文档

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT