説明
remove_shortcode() は、登録済みのショートコードを削除する関数です。
基本構文
remove_shortcode( string $tag ): void
引数
$tag:ショートコードの名前
戻り値
戻り値はありません。
使い方(サンプル)
remove_shortcode( 'gallery' );
注意点
- 登録が行われた後に実行します。
実務での使いどころ
- プラグインやテーマが、登録したショートコードを、無効にするとき。
- 別の処理に、差し替えるとき(解除してから、登録し直す)。
よくあるミスと対処
- 他のプラグインやテーマが登録するショートコードを、登録より前に実行してしまい、解除できない。
initフックの優先度を、大きくして呼ぶ(コア標準のgallery・captionは、早い段階で登録されるため、この問題は起きにくい)。 - 解除したあとも、本文に、
[タグ]の文字が、残る。 - タグの名前を、間違えている。
NG例
remove_shortcode( 'contact' ); // プラグインが init で登録する前に実行され、効かない
OK例
add_action( 'init', function () {
remove_shortcode( 'contact' );
add_shortcode( 'contact', 'my_contact' );
}, 20 );
使い分け早見表
| 関数 | 役割 |
|---|---|
remove_shortcode() |
ショートコードの登録を、解除する |
add_shortcode() |
ショートコードを、登録する |
shortcode_exists() |
ショートコードが、登録されているかを確認する |
関連項目
add_shortcode()、shortcode_exists()
公式リファレンス:remove_shortcode() | Function | WordPress Developer Resources

コメント