vg-i0w
Last Updated: February 25, 2016
·
7.038K
· hi_dm
 y icon

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>

Say Thanks
Respond

3 Responses
Add your response

7650
2a0e099239b4c3f98462f10883789bdd

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 ·
11946
 y icon

That link is broken.

over 1 year ago ·
13629
E455c3462c1c002b83f76f50cdcd689c normal

Thanks! This worked for me! :)

over 1 year ago ·