Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove empty p tags from WordPress posts
<?php
add_filter( 'the_content', 'remove_empty_p', 20, 1 );
function remove_empty_p( $content ){
// clean up p tags around block elements
$content = preg_replace( array(
'#<p>\s*<(div|aside|section|article|header|footer)#',
'#</(div|aside|section|article|header|footer)>\s*</p>#',
'#</(div|aside|section|article|header|footer)>\s*<br ?/?>#',
'#<(div|aside|section|article|header|footer)(.*?)>\s*</p>#',
'#<p>\s*</(div|aside|section|article|header|footer)#',
), array(
'<$1',
'</$1>',
'</$1>',
'<$1$2>',
'</$1',
), $content );
return preg_replace('#<p>(\s|&nbsp;)*+(<br\s*/*>)*(\s|&nbsp;)*</p>#i', '', $content);
}
@davidnknight

This comment has been minimized.

Copy link

davidnknight commented Apr 28, 2013

Doesn't work.

@jhogervorst

This comment has been minimized.

Copy link

jhogervorst commented Jul 1, 2013

Change the regex to <p>(\s|&nbsp;)*+(<br\s*/*>)?(\s|&nbsp;)*</p> to match &nbsp; as whitespace as well.

@mwordpress

This comment has been minimized.

Copy link

mwordpress commented Jan 5, 2014

thanks ninnypants and jhogervorst for fix
nice hack

@ninnypants

This comment has been minimized.

Copy link
Owner Author

ninnypants commented Feb 19, 2014

Added more support for block level elements recently will probably turn this into a plugin on .org just to make things easier

@deemi

This comment has been minimized.

Copy link

deemi commented Feb 9, 2015

OSM ... Work Perfectly

@CliffTam

This comment has been minimized.

Copy link

CliffTam commented Mar 11, 2015

Works great. Thanks for your work!

@mtinsley

This comment has been minimized.

Copy link

mtinsley commented May 16, 2015

This works great! I had one issue where a plugin was outputting HTML comments and that was interfering with the regex (#<p>\s*<(div|aside|section|article|header|footer)# doesn't match <p><!--foo--><div>). My solution was to just strip out all of the comments before applying this filter, using this regex <!--(.*?)-->.

@kprovance

This comment has been minimized.

Copy link

kprovance commented Sep 1, 2015

Seems WP 4.3 broke this? ANyone else?

@ShyamArtemas

This comment has been minimized.

Copy link

ShyamArtemas commented Sep 17, 2015

Use Jquery : $( 'p:empty' ).remove();

@jaychennai

This comment has been minimized.

Copy link

jaychennai commented Sep 23, 2015

jquery option is the best, add it to custom.js , under jquery document ready function

@ThornedRose

This comment has been minimized.

Copy link

ThornedRose commented Nov 28, 2015

@kprovance Does not work for me either on WP4.3.1 :(

@karsai5

This comment has been minimized.

Copy link

karsai5 commented Feb 9, 2016

Worked wonders for me. This will literally save me hours trying to hunt down empty lines.

@ThornedRose it seems to work again in 4.4.1

@JiveDig

This comment has been minimized.

Copy link

JiveDig commented Apr 4, 2016

Wow... this is really awesome. Thanks!

@dharmendrasdlt

This comment has been minimized.

Copy link

dharmendrasdlt commented Apr 21, 2016

The guy posted this. (You are awesome 👍 )

@yourdesigncoza

This comment has been minimized.

Copy link

yourdesigncoza commented Oct 29, 2016

Glad To Report That It's Still Working

@carlosesierra

This comment has been minimized.

Copy link

carlosesierra commented Oct 2, 2017

2017 and still working! Awesome work!

@miladghiravani

This comment has been minimized.

Copy link

miladghiravani commented Nov 2, 2017

Like this

@dirface

This comment has been minimized.

Copy link

dirface commented Jan 4, 2018

p:empty { display:none; }

@bradley2083

This comment has been minimized.

Copy link

bradley2083 commented Jan 15, 2018

Not working for me.. I have a shortcode that outputs a link along with a div container that is hidden with CSS and called when the link is clicked (as a modal). The div container, being a block element, causes the shortcode to end the paragraph early, show the div and then just has the remaining text sort of floating there unwrapped by anything.

So... let's say I have something like this in the tinymce:
Blah blah blah [popup id=5 text="click me to open the popup this should be inline with this blah blah paragraph"] and then the sentence continues.

The HTML output ends up being:

<p>Blah blah blah   </p>
<div id="popup-5" class="popup-modal">click me to open... paragraph</div>
and then the sentence continues.

Was hoping your function would get me output that looked like:
<p>Blah blah blah <div id="popup-5" class="popup-modal">click me to open... paragraph</div> and then the sentence continues.</p>

Any ideas? Thank you!

@EliW

This comment has been minimized.

Copy link

EliW commented Sep 10, 2019

Wanted to chime in to say 'thank you' for this gist. I was finally fed up with a crappy theme I'm using that aggressively added empty tags every time you hit save on a page. (Going to be moving away from the theme in the future, but gotta get through the next 2 months first). This is/was a life saver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.