WordPress: Function Reference/add filter:修订间差异
来自站长百科
无编辑摘要 |
无编辑摘要 |
||
第1行: | 第1行: | ||
== 描述== | == 描述== | ||
将一个函数hook到一个特别的filter action。 | 将一个函数hook到一个特别的filter action。 | ||
Filters是WordPress发行的hooks,用来更改不同类型的文本,再将这些文本添加到数据库或者发送到浏览器屏面。这时,插件可以指定一个或者更多的PHP函数使用Filter API,更改特别类型的文本。请看看[[WordPress:Plugin API|插件API]]上关于filter hooks的一个列表。 | |||
== 用法 == | == 用法 == | ||
%%% <?php add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%% | %%% <?php add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%% | ||
== 参数 == | == 参数 == | ||
{{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|当[[WordPress:Function Reference/apply_filters|应用]]filter的时候,调用的函数名。}} | {{Parameter|$function_to_add|callback|当[[WordPress:Function Reference/apply_filters|应用]]filter的时候,调用的函数名。}} | ||
{{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() 函数调用 运行时,设置的额外的参数。}} | |||
你可能要为同样的filter callbacks提供指针指向函数的名字空间。 | |||
你可能要为同样的filter | |||
%%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%% | %%% <?add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback'));?> %%% | ||
否则,WordPress会在自身名字空间查找函数,会导致不正常的操作。 | 否则,WordPress会在自身名字空间查找函数,会导致不正常的操作。 | ||
== 返回 == | == 返回 == | ||
如果<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>$function_to_add</tt>成功地加到了filter <tt>$tag</tt>上,返回<tt> |
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的函数。