php apply filters,apply_filters()应用过滤器

功能:

apply_filters()函数用来创建一个过滤器,大多数被用在函数中,是 WordPress 插件机制中非常重要的一个函数,能让其它的主题和插件对一个值进行修改过滤。

用法:

参数:

$tag

(字符串)(必须)过滤器的名字。

默认值:None

$value

(混合)(必须)要过滤的值,如果没人过滤则直接返回这个值。

$var

(混合) (可选)传给过滤函数额外的变量参数,辅助过滤函数对返回值进行操作,可以添加无限个。

返回值:

(混合)过滤后的值,如果没人过滤则直接返回 $value 的值。

所在位置:

apply_filters()函数包含在wp-includes/plugin.php中.

源码:

/**

* Call the functions added to a filter hook.

*

* The callback functions attached to filter hook $tag are invoked by calling

* this function. This function can be used to create a new filter hook by

* simply calling this function with the name of the new hook specified using

* the $tag parameter.

*

* The function allows for additional arguments to be added and passed to hooks.

*

* // Our filter callback function

* function example_callback( $string, $arg1, $arg2 ) {

* // (maybe) modify $string

* return $string;

* }

* add_filter( 'example_filter', 'example_callback', 10, 3 );

*

* /*

* * Apply the filters by calling the 'example_callback' function we

* * "hooked" to 'example_filter' using the add_filter() function above.

* * - 'example_filter' is the filter hook $tag

* * - 'filter me' is the value being filtered

* * - $arg1 and $arg2 are the additional arguments passed to the callback.

* $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );

*

* @since 0.71

*

* @global array $wp_filter Stores all of the filters.

* @global array $merged_filters Merges the filter hooks using this function.

* @global array $wp_current_filter Stores the list of current filters with the current one last.

*

* @param string $tag The name of the filter hook.

* @param mixed $value The value on which the filters hooked to `$tag` are applied on.

* @param mixed $var Additional variables passed to the functions hooked to `$tag`.

* @return mixed The filtered value after all hooked functions are applied to it.

*/

function apply_filters( $tag, $value ) {

global $wp_filter, $merged_filters, $wp_current_filter;

$args = array();

// Do 'all' actions first.

if ( isset($wp_filter['all']) ) {

$wp_current_filter[] = $tag;

$args = func_get_args();

_wp_call_all_hook($args);

}

if ( !isset($wp_filter[$tag]) ) {

if ( isset($wp_filter['all']) )

array_pop($wp_current_filter);

return $value;

}

if ( !isset($wp_filter['all']) )

$wp_current_filter[] = $tag;

// Sort.

if ( !isset( $merged_filters[ $tag ] ) ) {

ksort($wp_filter[$tag]);

$merged_filters[ $tag ] = true;

}

reset( $wp_filter[ $tag ] );

if ( empty($args) )

$args = func_get_args();

do {

foreach( (array) current($wp_filter[$tag]) as $the_ )

if ( !is_null($the_['function']) ){

$args[1] = $value;

$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));

}

} while ( next($wp_filter[$tag]) !== false );

array_pop( $wp_current_filter );

return $value;

}

示例:

echo apply_filters( 'test', '可以被修改的值' );

相关函数:

感觉本站内容不错,读后有收获?小额赞助

还可以分享文章给好友:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值