説明
doing_filter() は、指定したフィルターの実行中かどうかを判定する関数です。
基本構文
doing_filter( string|null $hook_name = null ): bool
引数
$hook_name:フィルター名。nullならどれか実行中かを判定
戻り値
実行中なら true。
使い方(サンプル)
if ( doing_filter( 'the_content' ) ) {
// the_content の途中
}
注意点
the_contentの中で再度the_contentを呼ぶ無限ループの回避に便利です。
実務での使いどころ
- あるフィルターの、適用の最中かを、確認するとき(再帰の防止など)。
よくあるミスと対処
- 「すでに適用された」かの確認に使っている。適用済みかは、
did_filter()。 - 引数を省略したときは、どのフィルターでも、実行中なら
true。特定のフィルターを調べるときは、名前を指定する。
NG例
add_filter( 'the_content', function ( $content ) {
return apply_filters( 'the_content', $content ); // 再帰して、無限ループになる
} );
OK例
function my_content_filter( $content ) {
remove_filter( 'the_content', 'my_content_filter' ); // 再帰を防ぐ
$out = apply_filters( 'the_content', $content );
add_filter( 'the_content', 'my_content_filter' );
return $out;
}
add_filter( 'the_content', 'my_content_filter' );
使い分け早見表
| 関数 | 役割 |
|---|---|
doing_filter() |
いま、適用中か |
did_filter() |
何回、適用されたか |
current_filter() |
いま、実行中のフィルター名 |
関連項目
doing_action()、current_filter()、did_filter()
公式リファレンス:doing_filter() | Function | WordPress Developer Resources

コメント