Skip to content

Instantly share code, notes, and snippets.

@mindpalette
Last active March 19, 2020 13:56
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 mindpalette/22b9d536bab6055e4a3871e5c3b0720f to your computer and use it in GitHub Desktop.
Save mindpalette/22b9d536bab6055e4a3871e5c3b0720f to your computer and use it in GitHub Desktop.
WordPress Filter - Trim Excerpt Whitespace
<?php
// trim excerpt whitespace
if ( !function_exists( 'mp_trim_excerpt_whitespace' ) ) {
function mp_trim_excerpt_whitespace( $excerpt ) {
return trim( $excerpt );
}
add_filter( 'get_the_excerpt', 'mp_trim_excerpt_whitespace', 1 );
}
@mindpalette
Copy link
Author

Sometimes when running the WordPress export/import tools, the excerpts will be empty at the new location. This is because the import tool doesn't remove whitespace from this field, and what was once empty is now a string of tabs and spaces. WordPress will see that the excerpt field has content, even if it's all whitespace, and will assume it's a custom excerpt. To fix, this function runs before the WP filter, and preemptively strips out leading and trailing whitespace from the excerpts.

@kensmash
Copy link

This was driving me bananas today. Thank you for this solution!!

@alstr
Copy link

alstr commented Mar 19, 2020

Very helpful, thanks!

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