WORDPRESSの記事内でコメントアウトを使うとpタグが挿入されるのを防ぐ

WORDPRESSの記事内で画像やコメントアウトを使うとpタグが挿入されるのが非常に面倒なので
pタグを削除する

function remove_p_on_images($content){
//画像のみpタグで囲わない
return preg_replace('/<p>(\s<em>)(<img .</em> \/>)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'remove_p_on_images');</p>
<p>function remove_p_on_commentout($content){
//コメントアウトのみpタグで囲わない
return preg_replace('/<p>(\s<em>)(<!-- .</em> -->)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'remove_p_on_commentout');</p>
<p>function remove_p_on_commentout2($content){
//コメントアウトのみpタグで囲わない
return preg_replace('/<p>(\s<em>)(<!--\/ .</em> -->)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'remove_p_on_commentout2');

これで削除できた。コメントアウトは今回2種類使っていたので2種類書いておいた。

WORDPRESS

Posted by bistro