Last active
March 19, 2020 13:56
-
-
Save mindpalette/22b9d536bab6055e4a3871e5c3b0720f to your computer and use it in GitHub Desktop.
WordPress Filter - Trim Excerpt Whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This was driving me bananas today. Thank you for this solution!!
Very helpful, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.