apply_filters

函数
apply_filters ( $hook_name, $value, $args )
参数
  • (string) $hook_name The name of the filter hook.
    Required:
  • (mixed) $value The value to filter.
    Required:
  • (mixed) $args Additional parameters to pass to the callback functions.
    Required:
返回值
  • (mixed) The filtered value after all hooked functions are applied to it.
定义位置
相关方法
apply_filters_ref_arrayapply_filters_deprecatedadd_filterhas_filterwp_list_filter
引入
0.71
弃用
-

apply_filters: 这个函数用于将一个或多个过滤器应用于一个给定的值。它需要两个参数:第一个是要过滤的值,第二个是要应用的过滤器的名称。开发人员可以使用这个函数在使用或显示数据之前对其进行修改或操作。

调用已添加到过滤器钩子的回调函数。

这个函数调用所有附加到过滤钩`$hook_name`的函数。可以通过简单地调用这个函数来创建新的过滤器钩子。
使用`$hook_name’参数指定新钩子的名称。

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

使用实例:

// The 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
* that’s hooked onto `example_filter` above.
*
* – ‘example_filter’ is the filter hook.
* – ‘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 );

function apply_filters( $hook_name, $value, ...$args ) {
	global $wp_filter, $wp_filters, $wp_current_filter;

	if ( ! isset( $wp_filters[ $hook_name ] ) ) {
		$wp_filters[ $hook_name ] = 1;
	} else {
		++$wp_filters[ $hook_name ];
	}

	// Do 'all' actions first.
	if ( isset( $wp_filter['all'] ) ) {
		$wp_current_filter[] = $hook_name;

		$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
		_wp_call_all_hook( $all_args );
	}

	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
		if ( isset( $wp_filter['all'] ) ) {
			array_pop( $wp_current_filter );
		}

		return $value;
	}

	if ( ! isset( $wp_filter['all'] ) ) {
		$wp_current_filter[] = $hook_name;
	}

	// Pass the value to WP_Hook.
	array_unshift( $args, $value );

	$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );

	array_pop( $wp_current_filter );

	return $filtered;
}

常见问题

FAQs
查看更多 >