remove empty paragraphs from the_content?

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?

Read More
/*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');

edit/update:

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">

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

</div>

any idea where this empty p’s come from?

Related posts

Leave a Reply

11 comments

  1. 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. 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:

    $('p:empty').remove();
    

    This works for me!

  3. 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*+(<brs*/*>)?s*</p>#i', '', $content);
    }
    

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

  4. 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.

  5. 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);
    
  6. 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

  7. 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.
                  '/<([^</>]*)>([s]*?|(?R))</1>/imsU',
    
                 //** Replace with nothing if string empty.
                 !is_string ($repto) ? '' : $repto,
    
                //** Source string
               $str
    );}
    

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

  8. 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.

  9. 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; ?>
    
  10. 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.

    PROBLEM

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

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

    SOLUTION

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

    <?php the_content();?>