Last Updated: February 25, 2016
·
11.28K
· hi_dm

Remove Empty Paragraph Tags in WordPress

WordPress is a fantastic CMS, but developers have long been troubled by unwanted, generated paragraph tags. In my case, I want WordPress to wrap p tags around blocks of text, but I find that sometimes, especially when inserting media, that empty paragraph tags are generated. The result looks something like this:

<p></p>

To destroy all empty paragraph tags, simply add the following block of code to your functions.php file:

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

<a href="https://gist.github.com/ninnypants/1668216" target="_blank">source</a>

3 Responses
Add your response

I couldn't get that code to work, but looking around I found this one that does the job.
Hope its useful. Thanks anyway

http://cdpn.io/xejEf

over 1 year ago ·

That link is broken.

over 1 year ago ·

Thanks! This worked for me! :)

over 1 year ago ·