Skip to main content Accessibility Feedback

Remove empty paragraphs added by WordPress

WordPress automatically adds paragraphs and line breaks to your content.

This is awesome when you’re trying to write a quick blog post… and infuriating when you’re trying to use specific, well-formed markup on a page. I’ve spent far too much time trying to prevent WordPress from adding empty <p> tags throughout my markup.

Fortunately, there’s an easy fix!

A simple helper method #

Just drop this snippet, courtesy of Ryan Hamilton, into your functions.php file:

 * Remove empty paragraphs created by wpautop()
 * @author Ryan Hamilton
 * @link
function remove_empty_p( $content ) {
    $content = force_balance_tags( $content );
    $content = preg_replace( '#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content );
    $content = preg_replace( '~\s?<p>(\s|&nbsp;)+</p>\s?~', '', $content );
    return $content;
add_filter('the_content', 'remove_empty_p', 20, 1);

I’ve added this to Keel. It’s a must-have feature for me.

🚀 Make 2018 the year you master JavaScript! My pocket guides and mini courses are short, focused, and made for beginners. You can do this!

Have any questions or comments about this post? Email me at or contact me on Twitter at @ChrisFerdinandi.

Get Daily Developer Tips