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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/65209/2a0e099239b4c3f98462f10883789bdd.jpg)
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
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/53081/_y-icon.jpg)
That link is broken.
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/102649/e455c3462c1c002b83f76f50cdcd689c_normal.jpeg)
Thanks! This worked for me! :)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Function
Authors
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/6486/chip-avatar.jpg)
Chip Castle
21.48K
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/23302/04b056e843c7f0e4c7e97e36174e43b4.jpeg)
deleteman
10.07K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#