How to remove empty paragraph tags in the_content()?

Last update: 21 January 2024
Empty tags in a WordPress post are removed with a simple function
How to remove empty paragraph tags in the_content()?

Add code in your file function.php

Tested:
WordPress 6.4.2
PHP 7.4

add_filter( 'the_content', 'wphaf_remove_empty_paragraph', 10, 1 );

function wphaf_remove_empty_paragraph( $content ) {
  return preg_replace( '#<p>(\s|&nbsp;)*+(<br\s*/*>)*(\s|&nbsp;)*</p>#i', '', $content );
}