Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Last active November 29, 2018 23:17
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 jazzsequence/4b531e81f16bd4870673efc3b6015fb1 to your computer and use it in GitHub Desktop.
Save jazzsequence/4b531e81f16bd4870673efc3b6015fb1 to your computer and use it in GitHub Desktop.
remove_empty_ps.php
<?php
/**
* Remove Empty Ps
*
* Deletes empty <p> tags on save.
* This can be included as a standalone plugin.
*/
namespace Gist\EmptyPs;
/**
* Kick off all the things.
*/
function bootstrap() {
add_action( 'save_post', __NAMESPACE__ . '\\save_p_less_content', 10, 3 );
add_filter( 'the_content', __NAMESPACE__ . '\\remove_empty_ps' );
}
/**
* When a post is saved, remove any empty <p> tags (or anything that would result in empty <p>
* tags, like carriage returns which render as &nbsp; in the database).
*
* @uses remove_empty_ps We use the remove_empty_ps function again in here after running the
* content through wpautop so we can scrape out empty lines.
* @param int $post_id The WP_Post ID.
* @param object $post The WP_Post object.
* @param bool $update Whether this is an update.
*/
function save_p_less_content( $post_id, $post, $update ) {
if ( wp_is_post_revision( $post ) || in_array( $post->post_type, [ 'attachment' ], true ) ) {
return;
}
// Remove the empty <p> tags by running the content through `wpautop` to wrap &nbsp;'s in <p>s.
$post_content = remove_empty_ps( wpautop( $post->post_content ) );
// Build up the new post.
$updated_post['ID'] = $post_id;
$updated_post['post_content'] = wp_kses_post( $post_content );
/*
* Danger, Will Robinson!
*
* Calling wp_update_post inside save_post has the potential to create an infinite loop.
* To solve this connundrum, we're un-hooking this function from save_post to prevent it from
* running when wp_update_post is run. We don't need to re-hook it, since we already hooked it
* in bootstrap().
*/
remove_action( 'save_post', __NAMESPACE__ . '\\save_p_less_content', 10, 3 );
wp_update_post( $updated_post );
}
/**
* This deals with empty blocks particularly on output and especially caused by shortcodes wrapped
* in <p> and <div> tags.
*
* @link https://gist.github.com/ninnypants/1668216
* @param string $content The post content.
* @return string The filtered post content.
*/
function remove_empty_ps( $content ) {
// Clean up <p> tags around block elements.
$content = preg_replace(
[
'#<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)#',
], [
'<$1',
'</$1>',
'</$1>',
'<$1$2>',
'</$1',
],
$content
);
return preg_replace( '#<p>(\s|&nbsp;)*+(<br\s*/*>)*(\s|&nbsp;)*</p>#i', '', $content );
}
// Engage!
bootstrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment