WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
See the question: I'm using a filter to remove the <p> tags auto wrap – Chris_O Apr 3 '11 at 12:13
Try running your filter before wpautop does it's thing, eg. add_filter('the_content', 'qanda', 7 );.. – t31os Apr 3 '11 at 22:16
thank you, that's the solution! perfect – mathiregister Apr 4 '11 at 8:51
@t31os: Can you move your comment to an answer so we can vote on it? – Jan Fabry Apr 6 '11 at 12:31
Answer added... :) – t31os Apr 6 '11 at 12:44
up vote 13 down vote accepted

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);
share|improve this answer
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! – mathiregister Apr 3 '11 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? – mathiregister Apr 3 '11 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!

share|improve this answer
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. – Sol Sep 6 '12 at 5:08
You can also do this with CSS: p:empty { display:none; } – D_N Jul 20 at 1:42

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

share|improve this answer
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. – goto10 Aug 14 '13 at 21:56

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.

share|improve this answer

Simply use CSS

p:empty {
  display: none;
share|improve this answer
Please add an explanation to your answer – Pieter Goosen Sep 30 '15 at 13:49
@PieterGoosen it is already self-explanatory – at least three characters Oct 1 '15 at 12:23
If you just want to avoid displaying them for spacing purposes, this works well down to IE9. caniuse.com/#feat=css-sel3 and developer.mozilla.org/en-US/docs/Web/CSS/%3Aempty for more. – Will Jan 3 at 21:11
nice option with CSS selector method, didn't know it existed. thanks! – i_a Apr 14 at 11:47

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

share|improve this answer
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. – mathiregister Apr 3 '11 at 14:54
I actuall wonder why my add_filter thingy does not work? – mathiregister Apr 3 '11 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 '11 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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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