0

I simply want to prevent the creation of empty paragraphs in my WordPress post.

function removepar($content) {
    $content = str_replace("<p></p>","",$content);
    return $content;
}

add_filter('the_content', 'removepar');

I want to know that why this does not work?

Note

I have found that I can solve this using remove_filter( 'the_content', 'wpautop' ), but my question is: Why my first filter isn't working?

  • Maybe the priority can be set with a higher priority, if <p></p> are added after your filter by another plugin. – Benoti Oct 29 '16 at 19:15
0

It's not working because:

  • Maybe your filter is running to early as Benoti said in his comments, your filter callback may run before the empty paragraphs are added.

  • Maybe you are dealing with something different than am empty <p></p>, think <br>, &nbsp;...

Solution 1:

Disable autop (remove it's filter).

Solution 2:

add_filter('the_content', 'wpse_244389');
function wpse_244389($content) {
    $pattern = '#<p>(\s|&nbsp;|</?\s?br\s?/?>)*</?p>#';
    $content = preg_replace( $pattern, '', $content );
    return $content;
}

See https://stackoverflow.com/a/14261024/358906

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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