apply_filters(...) 在 WordPress 中实际上做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2270989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:34:14  来源:igfitidea点击:

What does apply_filters(...) actually do in WordPress?

wordpressfunction

提问by Tim

I'm trying to understand some of the function in WordPress, but I can't get my head around what apply_filters(...)actually does.

我正在尝试了解 WordPress 中的一些功能,但我无法理解apply_filters(...) 的实际作用。

Is someone able to clear this up for me with a few examples?

有人能用几个例子为我澄清这一点吗?

采纳答案by Richard M

apply_filters($tag, $value)passes the 'value' argument to each of the functions 'hooked' (using add_filter) into the specified filter 'tag'. Each function performs some processing on the value and returns a modified value to be passed to the next function in the sequence.

apply_filters($tag, $value)将 'value' 参数传递给每个被 'hooked'(使用add_filter)的函数到指定的过滤器 'tag' 中。每个函数对值执行一些处理,并返回一个修改后的值以传递给序列中的下一个函数。

For example, by default (in WordPress 2.9) the the_contentfilter passes the value through the following sequence of functions:

例如,默认情况下(在 WordPress 2.9 中)the_content过滤器通过以下函数序列传递值:

  • wptexturize
  • convert_smilies
  • convert_chars
  • wpautop
  • shortcode_unautop
  • prepend_attachment
  • do_shortcode
  • 纹理化
  • convert_smilies
  • 转换字符
  • wpautop
  • 简码_unautop
  • prepend_attachment
  • do_shortcode

回答by kaiser

late answer

迟到的答复

Short explanation

简短说明

apply_filters()interacts with the global $wp_filtersarray. Basically it just checks the array if the current filter (or hook) has an action(/callback function) attached and then calls it.

apply_filters()global $wp_filters数组交互。基本上它只是检查当前过滤器(或钩子)是否附加了一个动作(/回调函数)然后调用它。

Long explanation

长解释

When you attach a callback/action to a filter or hook, then you just add the callback name to global filters array. When then, in code (for e.g. a template, core or plugin file) a call to do_action()or apply_filters()happens, then WordPress searched through the array and calls the callback. The only thing more special with filters than with hooks is, that it returns the value (for further handling) instead of just firing the callback. So summed up: Hooks are to insertdata, while filters are to modifydata.

当您将回调/操作附加到过滤器或挂钩时,您只需将回调名称添加到全局过滤器数组。然后,在代码(例如模板、核心或插件文件)中调用do_action()apply_filters()时,WordPress 会搜索数组并调用回调。过滤器比钩子更特别的是,它返回值(用于进一步处理)而不是仅仅触发回调。所以总结起来:Hooks 是插入数据,而filters 是修改数据。

回答by Marjorie Roswell

Here's what I'm gleaning, upon considering the most popular answer and additional resources:

这是我在考虑最受欢迎的答案和其他资源后所收集的信息:

  • $tag seems to be a synonym for the name of the hook. (That's not particularly intuitive to me.)
  • the_content is an example of a hook, of the "filter" type.
  • the_content hook consists of multiple filters.
  • Filters modify data. They basically filter the database, changing the data before the users view it.
  • A common use of apply_filters(), for instance, is to apply the_content filters to $content. In this instance, double returns will convert to <p>tags, smiley faces will convert to icons, etc.
  • "the_content" is a hook, while "the_content()" is a function.
  • $tag 似乎是钩子名称的同义词。(这对我来说并不是特别直观。)
  • the_content 是“过滤器”类型的钩子示例。
  • the_content 钩子由多个过滤器组成。
  • 过滤器修改数据。他们基本上过滤数据库,在用户查看数据之前更改数据。
  • 例如,apply_filters() 的一个常见用途是将 the_content 过滤器应用于 $content。在这种情况下,双重返回将转换为<p>标签,笑脸将转换为图标等。
  • “the_content”是一个钩子,而“the_content()”是一个函数。

回答by Steve

In the most basic terms, apply_filters is used to initialise a filter hook... add_filter assigns a new function to hooks that have already been created.

在最基本的术语中,apply_filters 用于初始化过滤器钩子... add_filter 为已经创建的钩子分配一个新函数。