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>
Written by &y
Related protips
3 Responses

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

That link is broken.
over 1 year ago
·

Thanks! This worked for me! :)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Function
Authors

Chip Castle
21.7K

deleteman
10.11K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#