WordPress: Function Reference/add filter:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第1行: 第1行:
== Description ==
== 描述==
== 描述==
Hooks a function to a specific filter action.
将一个函数hook到一个特别的filter action。
将一个函数hook到一个特别的filter action。


Filters are the hooks that WordPress launches to modify text of various types  before adding it to the database or sending it to the browser screen. Plugins  can specify that one or more of its PHP functions is executed to  modify specific types of text at these times, using the Filter API.  See the [[WordPress:Plugin API]] for a list of filter hooks.
Filters是WordPress发行的hooks,用来更改不同类型的文本,再将这些文本添加到数据库或者发送到浏览器屏面。这时,插件可以指定一个或者更多的PHP函数使用Filter API,更改特别类型的文本。请看看[[WordPress:Plugin API|插件API]]上关于filter hooks的一个列表。


Filters是WordPress发行的hooks,用来更改不同类型的文本,再将这些文本添加到数据库或者发送到浏览器屏面。这是,插件可以指定一个或者更多的PHP函数使用Filter API,更改特别类型的文本。请看看[[WordPress:Plugin API|插件API]]上关于filter hooks的一个列表。
== Usage ==
== 用法 ==
== 用法 ==
%%% <?php add_filter($tag,  $function_to_add,  $priority = 10,  $accepted_args = 1); ?> %%%


%%% <?php add_filter($tag,  $function_to_add,  $priority = 10,  $accepted_args = 1); ?> %%%
%%% <?php add_filter($tag,  $function_to_add,  $priority = 10,  $accepted_args = 1); ?> %%%


== Parameters ==
== 参数 ==
== 参数 ==
{{Parameter|$tag|string|The name of the filter to hook the <tt>$function_to_add</tt> to.}}


{{Parameter|$tag|string|The name of the filter to hook the <tt>$function_to_add</tt> to.}}
{{Parameter|$tag|string|The name of the filter to hook the <tt>$function_to_add</tt> to.}}
{{Parameter|$function_to_add|callback|The name of the function to be called when the filter is [[WordPress:Function Reference/apply_filters|applied]].}}


{{Parameter|$function_to_add|callback|当[[WordPress:Function Reference/apply_filters|应用]]filter的时候,调用的函数名。}}
{{Parameter|$function_to_add|callback|当[[WordPress:Function Reference/apply_filters|应用]]filter的时候,调用的函数名。}}
{{Parameter|$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 action.|option|10}}


{{Parameter|$priority|integer|用来指定,函数与一个特别的action相联合时的属性。较小的数字代表执行地较早,拥有同等优先级的函数的执行属性与它们添加到action上的顺序相同。|option|10}}
{{Parameter|$priority|integer|用来指定,函数与一个特别的action相联合时的属性。较小的数字代表执行地较早,拥有同等优先级的函数的执行属性与它们添加到action上的顺序相同。|option|10}}


{{Parameter|$accepted_args|integer|函数接受的参数的数目。在WordPress1.5.1版本以及更新的版本中。Hooked函数可以得到正在匹配的 do_action() or apply_filters() 函数调用 运行时,设置的额外的参数。}}


{{Parameter|$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 do_action() or apply_filters() call is run.}}
你可能要为同样的filter callbacks提供指针指向函数的名字空间。
 
{{Parameter|$accepted_args|integer|函数接受的参数的数目。在WordPress1.5.1版本以及更新的版本中。Hooked函数可以得到matching do_action() or apply_filters() call 运行时,设置的额外的参数。}}
 
 
 
You may need to supply a pointer to the function's namespace for some filter callbacks, e.g.
 
你可能要为同样的filter callbacks提供pointer指向函数的名字空间。


%%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%%
%%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%%
%%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%%
Otherwise WordPress looks in its own namespace for the function, which can cause abnormal behaviour.


否则,WordPress会在自身名字空间查找函数,会导致不正常的操作。
否则,WordPress会在自身名字空间查找函数,会导致不正常的操作。
== Return ==


== 返回 ==  
== 返回 ==  


 
如果<tt>$function_to_add</tt>成功地加到了filter <tt>$tag</tt>上,返回<tt>true</tt>。你的函数拥有多少个参数。在WordPress1.5.1+,hooked函数拥有正在匹配的<tt>do_action()</tt> 或者<tt>apply_filters()</tt>运行时,设置的额外的参数。例如,action <tt>comment_id_not_found</tt>会传递任何hook onto需要的评论ID的函数。
<tt>true</tt> if the <tt>$function_to_add</tt> is added successfully to filter <tt>$tag</tt>. How many arguments your function takes. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching <tt>do_action()</tt> or <tt>apply_filters()</tt> call is run. For example, the action <tt>comment_id_not_found</tt> will pass any functions that hook onto it the ID of the requested comment.
 
如果<tt>$function_to_add</tt>成功地加到了filter <tt>$tag</tt>上,返回<tt>正确的</tt>。你的函数拥有多少个参数。在WordPress1.5.1+,hooked函数拥有matching <tt>do_action()</tt> 或者<tt>apply_filters()</tt> call运行时,设置的额外的函数。例如,action <tt>comment_id_not_found</tt>会传递任何hook onto需要的评论ID的函数。

2008年7月26日 (六) 14:51的最新版本

描述[ ]

将一个函数hook到一个特别的filter action。

Filters是WordPress发行的hooks,用来更改不同类型的文本,再将这些文本添加到数据库或者发送到浏览器屏面。这时,插件可以指定一个或者更多的PHP函数使用Filter API,更改特别类型的文本。请看看插件API上关于filter hooks的一个列表。

用法[ ]

%%% <?php add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%%

参数[ ]

你可能要为同样的filter callbacks提供指针指向函数的名字空间。

%%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%%

否则,WordPress会在自身名字空间查找函数,会导致不正常的操作。

返回[ ]

如果$function_to_add成功地加到了filter $tag上,返回true。你的函数拥有多少个参数。在WordPress1.5.1+,hooked函数拥有正在匹配的do_action() 或者apply_filters()运行时,设置的额外的参数。例如,action comment_id_not_found会传递任何hook onto需要的评论ID的函数。