remove_theme_support() の使い方|説明・引数・注意点

説明

remove_theme_support() は、テーマ(や親テーマ)が有効にしている機能を無効にする関数です。

基本構文

remove_theme_support( string $feature ): bool

引数

  • $feature:機能名

戻り値

無効にできたら true。

使い方(サンプル)

remove_theme_support( 'widgets-block-editor' );

注意点

  • 親テーマで有効にされた機能を、子テーマで無効にするときに使います。
  • after_setup_theme で、親テーマより後の優先度で実行します。

実務での使いどころ

  • 親テーマが有効にした機能を、子テーマで、無効にするとき。
  • 不要な機能(例:core-block-patterns)を、外すとき。

よくあるミスと対処

  • 親テーマが add_theme_support() を呼ぶより前に、実行してしまい、外せない。子テーマでは、after_setup_theme フックの優先度を、大きく(11 など)する。
  • 外せない機能がある。すべての機能が、外せるわけではない。
  • 機能の名前を、間違えている。

NG例

remove_theme_support( 'post-thumbnails' ); // 親テーマが、有効にする前に実行され、効かない

OK例

add_action( 'after_setup_theme', function () {
	remove_theme_support( 'widgets-block-editor' );
}, 11 );

使い分け早見表

関数 役割
remove_theme_support() 有効にした機能を、無効にする
add_theme_support() テーマの機能を、有効にする
current_theme_supports() 機能が、有効かを確認する

関連項目

add_theme_support()、current_theme_supports()

公式リファレンス:remove_theme_support() | Function | WordPress Developer Resources

コメント

タイトルとURLをコピーしました