apply_filters()

apply_filters()函数是Wordpress函数,调用已添加到筛选器挂钩的回调函数。

apply_filters( string $tag, mixed $value )

说明(Description)

附加到筛选器挂钩的回调函数通过调用此函数来调用。这个函数可以用来创建一个新的过滤器钩子,只需使用使用$tag参数指定的新钩子的名称调用这个函数。

该函数还允许将多个附加参数传递给钩子。

示例用法:

//过滤器回调函数。

函数示例u回调($string,$arg1,$arg2){

//(可能)修改$string。

返回$string;

}

添加_filter(“example_filter”,“example_callback”,10,3);

/*

*通过调用“example_callback()”函数应用筛选器

*上面的“exampleu filter”就有这个功能。

*

*-“example_filter”是过滤器挂钩。

*-“filter me”是正在筛选的值。

*-$arg1和$arg2是传递给回调的附加参数。

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

参数(Parameters)

参数类型必填说明
$tag(string)必需筛选器挂钩的名称。
$value(mixed)必需要筛选的值。
$args(mixed)必需传递给回调函数的其他参数。

返回(Return)

(mixed)应用所有挂接函数后的过滤值。


源码(Source)

/**
 * 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;
}
更新版本源码位置使用被使用
0.71wp-includes/plugin.php:18111231 function

笔记(Notes)

滤波后回波
一个很容易忽略的基本参数是指定参数的数量。大多数过滤器只有一个参数,因此人们从add_filter中删除该参数。
从读取此函数的定义和描述中不明显的是,如果在代码中从未调用/执行add_filter(’filter_name’,’filter_function’),则默认情况下使用apply_filter(’filter_name’,$value)将返回$value的值。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索