remove empty paragraphs from the_content?

Written by  on September 30, 2016 

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

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?

Category : Wordpress

Tags :