-
-
Save mtinsley/ad57ebed545a4f13170a to your computer and use it in GitHub Desktop.
Removes empty paragraphs from the post content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Forked From: https://gist.github.com/ninnypants/1668216 | |
* | |
* Modified to handle comments before or after an element. | |
*/ | |
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*<!--(.*?)-->#', | |
'#<!--(.*?)-->\s*</p>#', | |
'#<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( | |
'<p>', | |
'</p>', | |
'<$1', | |
'</$1>', | |
'</$1>', | |
'<$1$2>', | |
'</$1', | |
), $content ); | |
return preg_replace('#<p>(\s| )*+(<br\s*/*>)*(\s| )*</p>#i', '', $content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment