WordPress開(kāi)發(fā)函數(shù)apply_filters()
WordPress開(kāi)發(fā)函數(shù)apply_filters(),調(diào)用已添加到篩選器鉤子中的回調(diào)函數(shù)。
用法:
apply_filters( string $tag, mixed $value )
描述
附加在過(guò)濾器鉤子上的回調(diào)函數(shù)是通過(guò)調(diào)用這個(gè)函數(shù)來(lái)調(diào)用的。通過(guò)使用$tag參數(shù)指定的新鉤子的名稱調(diào)用這個(gè)函數(shù),這個(gè)函數(shù)可以用來(lái)創(chuàng)建一個(gè)新的篩選器鉤子。
該函數(shù)還允許將多個(gè)附加參數(shù)傳遞給鉤子。
使用示例:
// 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 );
參數(shù);
$tag
(string) (必需) 過(guò)濾器鉤子的名稱。
$value
(mixed) (必需) 要過(guò)濾的值。
$args
(mixed) (必需) 傳遞給回調(diào)函數(shù)的附加參數(shù)。
返回
(mixed)所有鉤子函數(shù)應(yīng)用到過(guò)濾后的值。
來(lái)源:
文件: 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;
}
更新日志:

用戶貢獻(xiàn)的筆記
(由Codex - 6年前貢獻(xiàn))
回聲后過(guò)濾
echo apply_filters( $tag, $value );
得到過(guò)濾
$myvar = apply_filters( $tag, $value );
額外的過(guò)濾器參數(shù)
$myvar = apply_filters( $tag, $value, $param, $otherparam );
例如:
$myvar = apply_filters( 'example_filter', 'filter me', 'arg1', 'arg2 ');
與標(biāo)題過(guò)濾器這樣
$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過(guò)濾器貢獻(xiàn))
一個(gè)很容易忽略的基本參數(shù)是指定參數(shù)的數(shù)量。大多數(shù)過(guò)濾器只有一個(gè)參數(shù),因此人們從add_filter刪除參數(shù)。
3、以下幾點(diǎn)非常重要。
add_filter( 'example_filter', 'example_callback', 10, 3 );
否則你會(huì)得到以下錯(cuò)誤,例如這個(gè)StackOverflow問(wèn)題(http://stackoverflow.com/questions/24198086/missing-argument-2-for-the-function-in-wordpress):
Missing argument 2 for example_callback()
(由皮克網(wǎng)站貢獻(xiàn)- 2年前)
從這個(gè)函數(shù)的定義和描述中可以看出,如果add_filter('filter_name', 'filter_function')在代碼中從未被調(diào)用/執(zhí)行,使用apply_filters('filter_name', $value)將默認(rèn)返回$value的值。
我注意到一個(gè)調(diào)用apply_filters('my_filter',…)在我的主題函數(shù)。php文件中,找不到類似的add_filter('my_filter',…)打電話到其他任何地方,并對(duì)此感到困惑(我仍然纏繞著我的頭過(guò)濾器/鉤子)。
關(guān)于這一點(diǎn)的更多細(xì)節(jié)可以在本頁(yè)面的示例中看到:https://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers關(guān)于過(guò)濾器一節(jié)。
(由tampadave提供- 5年前)
apply_filters ( string $tag, mixed $value );
function apply_filters( $tag, $value )
參數(shù):
$tag - (string) (Required)過(guò)濾器鉤子的名稱。
$value -(mixed)(必需的)綁定到$tag的過(guò)濾器所應(yīng)用的值。
$var -(mixed)(必需的)附加變量傳遞給與$tag掛鉤的函數(shù)。
正確的。提示:如果你錯(cuò)過(guò)了,這沒(méi)有意義。