Hey guys, I simply want to prevent the creation of empty paragraphs in my wordpress post. That happens quite often when trying to manually space content.

I don't know why this doesn't take effect?

/*Remove empty paragraph tags from the_content*/
function removeEmptyParagraphs($content) {

    /*$pattern = "/<p[^>]*><\\/p[^>]*>/";   
    $content = preg_replace($pattern, '', $content);*/
    $content = str_replace("<p></p>","",$content);
    return $content;

add_filter('the_content', 'removeEmptyParagraphs');


seems like the problem is this:

function qanda($content) {

    // filters for [q=some question] and [a=some answer]
    // wraps it inside of <div class="qanda"><div class="question"> </div><div class="answer"> </div></div>
    $content = preg_replace('/\[q=(.+?)].+?\[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $content);

    return $content;

add_filter('the_content', 'qanda');

i did this function myself to filter for a kind of shortcode pattern in my posts and pages. Even though in my backend the post is completely done without paragraphs and unnecessary spacings the outcome looks like this:

<div class="entry">

    <div class="qanda">...</div>
    <div class="qanda">...</div>
    <div class="qanda">...</div>


any idea where this empty p's come from?


11 Answers 11


WordPress will automatically insert <p> and </p> tags which separate content breaks within a post or page. If, for some reason, you want or need to remove these, you can use either of the following code snippets.

To completely disable the wpautop filter, you can use:

remove_filter('the_content', 'wpautop');

If you still want this to function try adding a later priority value to your filter something like:

add_filter('the_content', 'removeEmptyParagraphs',99999);
  • 2
    thank you! I know wordpress automatically inserts p tags. However there happen some cases where there are just empty <p></p> tags somewhere in my content (when i inspect it with some tool)... that happens when doing a lot of removal and editing of posts. I just don't want to have empty paragraphs in my content, that's all. I do need paragraphs, just not empty ones. The 99999 doesn't make a difference. Just doesn't work. the wpautop filter is not what I want. It prevents all <p>'s and autoformatting. I just don't want any empty p's! Apr 3, 2011 at 12:23
  • i updated my post so you see what I mean! i did a function that already filters the content. it inserts divs and it seemes wordpress is inserting <p></p> before and after it, i just don't get it. any ideas? Apr 3, 2011 at 13:02

I had the same problem you have. I just did a... let's say... not very beautiful solution, but it works and so far it's the only solution I have. I added a little JavaScript line. It needs jQuery, but I'm sure you can figure it out without.

This is my tiny JS:


This works for me!

  • oh aint that a sweet little number! Thanks for the tip - it works for me and in case anyone else wondered how to use it, just put it in your theme's custom JS file. Sep 6, 2012 at 5:08
  • 17
    You can also do this with CSS: p:empty { display:none; }
    – user68816
    Jul 20, 2016 at 1:42
  • 1
    @D_N Using CSS to hide empty Paragraph tags only works for <p></p> but doesn't work for <p>\n</p>. Apr 21, 2017 at 20:51

Simply use CSS

p:empty {
  display: none;

I know this is already marked 'solved' but just for reference, here's a function which does exactly what you want without having to add any markup to posts. Just put this in your theme's functions.php:

add_filter('the_content', 'remove_empty_p', 20, 1);
function remove_empty_p($content){
    $content = force_balance_tags($content);
    return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);

This is from this gist: https://gist.github.com/1668216

  • 6
    Just a little note about using force_balance_tags()... I ran into a tricky bug caused by this function when it was used on content that included JavaScript (JS was coming from Gravity Forms when using ajax on a form). There are known problems with force_balance_tags when it encounters the < character in certain situations. See ticket [9270]( core.trac.wordpress.org/ticket/9270) for details. Aug 14, 2013 at 21:56
  • I had the same problem highlighted by Dave: the snippet removed embedded youtube video and that caused validation problems on amp pages also. Jun 29, 2019 at 12:14

You could just run your filter before that nasty wpautop hooks on and messes with the markup.

add_filter('the_content', 'qanda', 7 );

That way, you've already converted what you need to by the time it hooks on, which does help in some cases.


Same approach than 2 answers before me, but an updated regex, because his didn't work for me.

the regex: /<p>(?:\s|&nbsp;)*?<\/p>/i (non capture group looking for any number of either whitespace or &nbsp;s inside p-tag, all case insenstive.

add_filter('the_content', function($content) {
    $content = force_balance_tags($content);
    return preg_replace('/<p>(?:\s|&nbsp;)*?<\/p>/i', '', $content);
}, 10, 1);

I found this weird, but actually calling the_content() will insert paragraphs in the manner you describe. If you want the html code, basically like you entered it (the same as "view HTML" when editing), then use get_the_content() which returns the content without formatting and paragraph tags.

Since it returns it, make sure you use something like:

echo get_the_content();

See also: http://codex.wordpress.org/Function_Reference/get_the_content

  • well, thank you. However I don't want that! I need normal paragraphs. First of it's semantic markup and secondly it's just the way it's supposed to. I just don't to have empty paragraphs that don't make sense! Simply because I have styling applied to those paragraphs also empty paragraphs appear with this styling and my page looks weird. Apr 3, 2011 at 14:54
  • I actuall wonder why my add_filter thingy does not work? Apr 3, 2011 at 14:55
  • Gotcha. Well one thing I would recommend trying is switching from HTML to visual and back a time or two. I believe when the WYSIWYG editor loads it does remove empty paragraph tags.
    – cwd
    Apr 4, 2011 at 3:11

This will recursively remove all the empty html tags from the string

add_filter('the_content', 'remove_empty_tags_recursive', 20, 1);
function remove_empty_tags_recursive ($str, $repto = NULL) {
         $str = force_balance_tags($str);
         //** Return if string not given or empty.
         if (!is_string ($str)
         || trim ($str) == '')
        return $str;

        //** Recursive empty HTML tags.
       return preg_replace (

              //** Pattern written by Junaid Atari.

             //** Replace with nothing if string empty.
             !is_string ($repto) ? '' : $repto,

            //** Source string

Pattern is taken from http://codesnap.blogspot.in/2011/04/recursively-remove-empty-html-tags.html


If you have <p> tags with whitespace in the content, go to your post or page an edit it not in visual style.

you would be find some &nbsp; in there.. Delete it and the empty <p> tags will disappear.


In order to have only html content without

tags we can use the following loop to out put only the html without formatting of the post or page

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php echo $post->post_content; ?>
<?php endwhile; endif; ?>

If you are getting an empty <p> tag at the top of your page or post, it is usually caused by wrapping the <?php the_content();?> in paragraph tags.


This is what causes the stray <p> tag at the top of the page/post:

<p><?php the_content();?></p>


If you remove the <p> tags and only have the template tag, you'll solve the problem.

<?php the_content();?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.