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

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

function remove_p_on_images($content){
//画像のみpタグで囲わない
    return preg_replace('/

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

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

(\s*)(<\!\-\-\/ .* \-\->)(\s*)<\/p>/iU', '\2', $content); } add_filter('the_content', 'remove_p_on_commentout2');

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

WORDPRESS

Posted by bistro