Skip to content

Instantly share code, notes, and snippets.

@Fantikerz
Forked from ninnypants/remove-empty-p.php
Last active June 22, 2017 06:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Fantikerz/5557617 to your computer and use it in GitHub Desktop.
Save Fantikerz/5557617 to your computer and use it in GitHub Desktop.
WordPress' wpautop adds line breaks and empty paragraphs in some cases, especially when the first item in the post is an image. This attempts to strip those tags which may be desirable if you have special paragraph styling or have a consistently desired post format.
<?php
function remove_empty_p($content)
{
$content = force_balance_tags($content);
$return = preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
$return = preg_replace('~\s?<p>(\s|&nbsp;)+</p>\s?~', '', $return);
return $return;
}
add_filter('the_content', 'remove_empty_p', 20, 1);
?>
@dskvr
Copy link

dskvr commented Jun 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment