Skip to content

Instantly share code, notes, and snippets.

@lenivene
Created February 4, 2016 13:59
Show Gist options
  • Save lenivene/47b242627503304233da to your computer and use it in GitHub Desktop.
Save lenivene/47b242627503304233da to your computer and use it in GitHub Desktop.
Remove multiple spaces and filter in the_content and the_excerpt || get_the_excerpt.
<?php
/*
* Filter in functions 'the_content' and 'the_excerpt'
*/
add_filter( 'get_the_excerpt', 'esc_spaces' );
// add_filter( 'the_excerpt', 'esc_spaces' ); // Don't required
add_filter( 'the_content', 'esc_spaces' );
function esc_spaces( $content ){
if ( '' == $content )
return $content;
$safe_content = wp_check_invalid_utf8( $content );
$safe_content = preg_replace( array(
'/\s{2,}/',
'/[\t\n]/'
), ' ', $safe_content );
if ( '' === $safe_content ) {
return $safe_content;
}
/**
* Filter the content remove multiple spaces.
*
* @param string $content The content prior to being escaped.
* @param string $safe_content The text after it has been escaped.
*/
return apply_filters( 'spaces_escape', $safe_content, $content );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment