【即解決】WordPressのショートコードがpタグで囲まれてしまうのを最も簡単に除去する方法

WordPressの自作ショートコードを設定した時に、ショートコードの次に改行を入れて文章を入力するとpタグによって囲まれることがある。

そのような時の解決方法。以下のコードをfunctions.phpに挿入する。

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );

他にもいろいろごちゃごちゃコードを書く方法があるけど、これが一番シンプル。

ショートコードの時だけpタグの自動挿入をしないように設定している。

参考:remove empty <p> tags from wordpress shortcodes via a php functon

さらに空のpタグを除去する方法はこちらのコードで除去が可能。

function tgm_io_shortcode_empty_paragraph_fix( $content ) {
 
    $array = array(
        '<p>['    => '[',
        ']</p>'   => ']',
        ']<br />' => ']'
    );
    return strtr( $content, $array );
 
}
add_filter( 'the_content', 'tgm_io_shortcode_empty_paragraph_fix' );

参考:How To Remove Empty Paragraph Tags From Shortcodes In WordPress