diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 7398d57249..512d17e67b 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3728,7 +3728,7 @@ function wp_trim_excerpt( $text = '', $post = null ) { * * @param int $number The maximum number of words. Default 55. */ - $excerpt_length = apply_filters( 'excerpt_length', $excerpt_length ); + $excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length ); /** * Filters the string in the "more" link displayed after a trimmed excerpt. @@ -3773,6 +3773,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) { $original_text = $text; $text = wp_strip_all_tags( $text ); + $num_words = (int) $num_words; /* * translators: If your word count is based on single characters (e.g. East Asian characters), diff --git a/tests/phpunit/tests/formatting/WPTrimWords.php b/tests/phpunit/tests/formatting/WPTrimWords.php index fab62372f7..7b7438de46 100644 --- a/tests/phpunit/tests/formatting/WPTrimWords.php +++ b/tests/phpunit/tests/formatting/WPTrimWords.php @@ -73,4 +73,14 @@ class Tests_Formatting_WPTrimWords extends WP_UnitTestCase { restore_previous_locale(); $this->assertEquals( $expected, $actual ); } + + /** + * @ticket 47867 + */ + function test_works_with_non_numeric_num_words() { + $this->assertEquals( '', wp_trim_words( $this->long_text, '', '' ) ); + $this->assertEquals( '', wp_trim_words( $this->long_text, 'abc', '' ) ); + $this->assertEquals( '', wp_trim_words( $this->long_text, null, '' ) ); + $this->assertEquals( 'Lorem ipsum dolor', wp_trim_words( $this->long_text, '3', '' ) ); + } }