apply_filters()
構文
apply_filters(①$tag,②$value,③$var[オプション])
参照:WordPress Codex-apply_filters()
apply_filters()の詳細
動 作
新規のフィルターフックを作成する。
返り値
(mixed)
第2引数$valueに全てのフィルター関数を適用した値。
$valueの型と同じでなければならない。
全ての関数を適用した第2引数$valueの値。
引 数
①$tag
(文字列・必須) フィルターフック名
②$value
(mixed・必須) $tagに登録されたフィルター関数において変更できる値。
③$var
(mixed・オプション)フィルター関数へ渡すことのできる追加の変数。
関数内で使用はできるがこの変数にかかる値が返されることはない。
apply_filters()のコード
<?php 143 /** 144 * Calls the callback functions that have been added to a filter hook. 145 * 146 * The callback functions attached to the filter hook are invoked by calling 147 * this function. This function can be used to create a new filter hook by 148 * simply calling this function with the name of the new hook specified using 149 * the `$tag` parameter. 150 * 151 * The function also allows for multiple additional arguments to be passed to hooks. 152 * 153 * Example usage: 154 * 155 * // The filter callback function 156 * function example_callback( $string, $arg1, $arg2 ) { 157 * // (maybe) modify $string 158 * return $string; 159 * } 160 * add_filter( 'example_filter', 'example_callback', 10, 3 ); 161 * 162 * /* 163 * * Apply the filters by calling the 'example_callback()' function that's 164 * * hooked onto `example_filter` above. 165 * * 166 * * - 'example_filter' is the filter hook 167 * * - 'filter me' is the value being filtered 168 * * - $arg1 and $arg2 are the additional arguments passed to the callback. 169 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); 170 * 171 * @since 0.71 172 * 173 * @global array $wp_filter Stores all of the filters. 174 * @global array $wp_current_filter Stores the list of current filters with the current one last. 175 * 176 * @param string $tag The name of the filter hook. 177 * @param mixed $value The value to filter. 178 * @param mixed ...$args Additional parameters to pass to the callback functions. 179 * @return mixed The filtered value after all hooked functions are applied to it. 180 */ 181 function apply_filters( $tag, $value ) { 182 global $wp_filter, $wp_current_filter; 183 184 $args = func_get_args(); 185 186 // Do 'all' actions first. 187 if ( isset( $wp_filter['all'] ) ) { 188 $wp_current_filter[] = $tag; 189 _wp_call_all_hook( $args ); 190 } 191 192 if ( ! isset( $wp_filter[ $tag ] ) ) { 193 if ( isset( $wp_filter['all'] ) ) { 194 array_pop( $wp_current_filter ); 195 } 196 return $value; 197 } 198 199 if ( ! isset( $wp_filter['all'] ) ) { 200 $wp_current_filter[] = $tag; 201 } 202 203 // Don't pass the tag name to WP_Hook. 204 array_shift( $args ); 205 206 $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); 207 208 array_pop( $wp_current_filter ); 209 210 return $filtered; 211 } ?>