ショートコードの中ででエスケープコードを使用せずに、表示が崩れていることに気づかないまま公開してしまう事もあるかと思います。
今回紹介するエスケープ方法は、プラグインを使用せずショートコード用のタグを追加することで、タグ内で自動的にタブを「半角スペース」にし、「<」や「>」をそのまま表示させることが出来るのでとても便利です。
INDEX
ショートコードの記号をエスケープする
-
functions.phpに以下の関数を追加する。
- functions.php
- [code]
function escape_short_code( $attr, $content = null ){
$content = clean_pre($content);
$content = trim($content);
$content = str_replace(“\t”, ‘ ‘, $content);
$content = str_replace(‘<', '<', $content); $content = str_replace('>‘, ‘>’, $content);
return ‘'.$content.'
‘;
}
add_shortcode(‘code’, ‘escape_short_code’);
[/code]
-
ショートコード部分を【code】【/code】で挟み込む(【】は半角の大括弧にして下さい)
- HTML
- [code]
【code】…【/code】
[/code]
まとめ
WordPressのショートコード用プラグインをなるべく使用したくない方にとっては、このエスケープの方法はオススメです。