Im trying to modify the wp_trim_words function to return the left over words as well as the first part, any help much appreciated.
function wp_trim_words_new( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '…' );
$original_text = $text;
$text = wp_strip_all_tags( $text );
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[nrt ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
} else {
$words_array = preg_split( "/[nrt ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* @since 3.3.0
*
* @param string $text The trimmed text.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. ….
* @param string $original_text The text before it was trimmed.
*/
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}
It seems to me that you simply copied the
trim_words()
function of WordPress. You could have done better. Your desired output is unclear.Supposing that you would be happy with the simple tag-less string of left-over words returned together with the output of the original function, you could use something like this (untested):
to be called like this:
I’m not sure about
apply_filters()
, though. Should it be passed the$rest
too? It’s too time-consuming for me to study this one, sorry.If, on the other hand, you wanted the
$rest
before tag removal (i.e., before the call towp_strip_all_tags()
), then things are much more complicated, because the original functions splits the input into words after that, and I can think only of inefficient ways to keep the correspondence between the input and the output ofwp_strip_all_tags()
.BTW, the original code by WordPress contains a bug: words for East Asian languages are wrongly counted (and potentially split!) as single Unicode characters instead of Unicode graphemes. The fix is to use
'/X/u'
instead of'/./u'
(read here for an explanation).Not sure you need to tweak that function at all, this code does what you ask:
or wrap it in a function behaving as you wanted initially
You can accomplish this without the
wp_trim_words
function.