Removing unneccessary p-tags (Not every p)

I removed wpautop from my theme:

function disable_linebreaks($content) {
       remove_filter ('the_content','wpautop');
       return $content;
}
add_filter('the_content','disable_linebreaks',1);

But now, when switching between HTML and Editor ALL p-tags disappear, even the ones I coded myself.

Read More

What I want is just to prevent WordPress from adding empty <p></p> – Code. Basically I’m happy with wpautop, it’s just that sometimes it’s too much (For example around images. There’s always an empty p before every image).

Related posts

1 comment

  1. If what you want is really only

    to prevent WordPress from adding empty <p></p>

    and you’d be happy with removing those after post retrieval from the database, then

    function wpse108194_remove_empty_paragraphs( $content ) {
       $content = preg_replace( '#<p>s*</p>#', '', $content );
       return $content;
    }
    add_filter( 'the_content', 'wpse108194_remove_empty_paragraphs', 11 );
    

    will do.

Comments are closed.