Wordpress - Remove Empty paragraph tags with (nbsp)  from the_content()


/ Published in: PHP
Save to your folder(s)

Remove Wordpress empty paragraphs form the_content() function.


Copy this code and paste it in your HTML
  1. add_filter('the_content', 'remove_empty_tags_recursive', 20, 1);
  2. function remove_empty_tags_recursive ($str, $repto = NULL) {
  3. $str = force_balance_tags($str);
  4. //** Return if string not given or empty.
  5. if (!is_string ($str)
  6. || trim ($str) == '')
  7. return $str;
  8.  
  9. //** Recursive empty HTML tags.
  10. return preg_replace (
  11.  
  12. //** Pattern written by Junaid Atari.
  13. '~\s?<p>(\s|&nbsp;)+</p>\s?~',
  14.  
  15. //** Replace with nothing if string empty.
  16. !is_string ($repto) ? '' : $repto,
  17.  
  18. //** Source string
  19. $str
  20. );}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.