WordPress开发函数apply_filters(),调用已添加到筛选器钩子中的回调函数。

用法:

apply_filters( string $tag, mixed $value )

描述

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

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

使用示例:

// 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 );

参数;

$tag

(string) (必需) 过滤器钩子的名称。

$value

(mixed) (必需) 要过滤的值。

$args

(mixed) (必需) 传递给回调函数的附加参数。

返回

(mixed)所有钩子函数应用到过滤后的值。

来源:

文件: wp-includes/plugin.php

function apply_filters( $tag, $value ) {

global $wp_filter, $wp_current_filter;

$args = func_get_args();

// Do 'all' actions first.

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

$wp_current_filter[] = $tag;

_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;

}

// Don't pass the tag name to WP_Hook.

array_shift( $args );

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

array_pop( $wp_current_filter );

return $filtered;

}

更新日志:
WordPress开发函数apply_filters() (https://www.wpzt.net/) WordPress开发教程 第1张
用户贡献的笔记

(由Codex - 6年前贡献)

回声后过滤

echo apply_filters( $tag, $value );

得到过滤

$myvar = apply_filters( $tag, $value );

额外的过滤器参数

$myvar = apply_filters( $tag, $value, $param, $otherparam );

例如:

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

与标题过滤器这样

$my_custom_title = apply_filters('the_title', ' My Custom Title (tm) ');

$my_custom_title will now contain 'My Custom Title ™', since the_title filter applies wptexturize() and trim(), among others.

(由icc97 - 4年前随the_title过滤器贡献)

一个很容易忽略的基本参数是指定参数的数量。大多数过滤器只有一个参数,因此人们从add_filter删除参数。

3、以下几点非常重要。

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

否则你会得到以下错误,例如这个StackOverflow问题(http://stackoverflow.com/questions/24198086/missing-argument-2-for-the-function-in-wordpress):

Missing argument 2 for example_callback()

(由皮克网站贡献- 2年前)

从这个函数的定义和描述中可以看出,如果add_filter('filter_name', 'filter_function')在代码中从未被调用/执行,使用apply_filters('filter_name', $value)将默认返回$value的值。

我注意到一个调用apply_filters('my_filter',…)在我的主题函数。php文件中,找不到类似的add_filter('my_filter',…)打电话到其他任何地方,并对此感到困惑(我仍然缠绕着我的头过滤器/钩子)。

关于这一点的更多细节可以在本页面的示例中看到:https://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers关于过滤器一节。

(由tampadave提供- 5年前)

apply_filters ( string $tag, mixed $value );

function apply_filters( $tag, $value )

参数:

$tag - (string) (Required)过滤器钩子的名称。

$value -(mixed)(必需的)绑定到$tag的过滤器所应用的值。

$var -(mixed)(必需的)附加变量传递给与$tag挂钩的函数。

正确的。提示:如果你错过了,这没有意义。