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?

/*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?


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);

Leave a Comment