Skip to content

Instantly share code, notes, and snippets.

@deemi
Forked from ninnypants/remove-empty-p.php
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deemi/129f353a3d0dd986817d to your computer and use it in GitHub Desktop.
Save deemi/129f353a3d0dd986817d to your computer and use it in GitHub Desktop.
<?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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment