[wp-trac] [WordPress Trac] #40482: apply_filters() for 'get_the_excerpt' doesn't consistently trim the excerpt

WordPress Trac noreply at wordpress.org
Wed Apr 19 11:49:44 UTC 2017


#40482: apply_filters() for 'get_the_excerpt' doesn't consistently trim the excerpt
--------------------------+-----------------------------
 Reporter:  AD_Taylor     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  4.7.3
 Severity:  normal        |   Keywords:
  Focuses:  template      |
--------------------------+-----------------------------
 When filtering a custom string with `'get_the_excerpt'` the resulting text
 isn't trimmed to the excerpt length as expected.

 Looking at the core, this is because the `wp_trim_excerpt` function (wp-
 includes/formatting.php:3288) '''only''' trims the `$text` when a blank
 string is passed to it. This seems like unexpected behaviour for a
 function specifically named to trim strings.

 To reproduce, create a post with the following content:

 ---
 Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean
 odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum
 rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis
 diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec
 fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget
 convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer
 sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor
 pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu
 pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at
 egestas purus egestas fermentum.

 Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed
 sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus.
 Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla
 et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci
 et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu
 gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas
 augue quis enim varius, ut sagittis massa auctor.
 ---

 Then place this code into the single.php template file:

 {{{
 <?php

 // Output 1
 the_excerpt();

 $text = "Etiam laoreet libero sit amet sem tempor, vel dictum odio
 bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum
 lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta
 urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet
 blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum.
 Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo.
 Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet
 dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu
 pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at
 egestas purus egestas fermentum.

 Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed
 sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus.
 Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla
 et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci
 et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu
 gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas
 augue quis enim varius, ut sagittis massa auctor.";

 // Output 2
 $text = apply_filters( 'get_the_excerpt', $text );
 $text = apply_filters( 'the_excerpt', $text );
 echo $text;

 ?>
 }}}

 When viewing the post `Output 2`, on version 4.7.3, will not be trimmed.

 The expected result is that the output from `Output 1` & `Output 2` is the
 same.

 To produce the expected result, the `wp_trim_excerpt` function should be
 changed to the following snippet, so that the lines responsible for
 trimming are outside the `if()` statement.

 {{{
 /**
  * Generates an excerpt from the content, if needed.
  *
  * The excerpt word amount will be 55 words and if the amount is greater
 than
  * that, then the string ' […]' will be appended to the excerpt. If
 the string
  * is less than 55 words, then the content will be returned as is.
  *
  * The 55 word limit can be modified by plugins/themes using the {@see
 'excerpt_length'} filter
  * The ' […]' string can be modified by plugins/themes using the
 {@see 'excerpt_more'} filter
  *
  * @since 1.5.0
  *
  * @param string $text Optional. The excerpt. If set to empty, an excerpt
 is generated.
  * @return string The excerpt.
  */
 function wp_trim_excerpt( $text = '' ) {
         $raw_excerpt = $text;
         if ( '' == $text ) {
                 $text = get_the_content('');

                 $text = strip_shortcodes( $text );

                 /** This filter is documented in wp-includes/post-
 template.php */
                 $text = apply_filters( 'the_content', $text );
                 $text = str_replace(']]>', ']]>', $text);
         }

         /**
          * Filters the number of words in an excerpt.
          *
          * @since 2.7.0
          *
          * @param int $number The number of words. Default 55.
          */
         $excerpt_length = apply_filters( 'excerpt_length', 55 );
         /**
          * Filters the string in the "more" link displayed after a trimmed
 excerpt.
          *
          * @since 2.9.0
          *
          * @param string $more_string The string shown within the more
 link.
          */
         $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]'
 );
         $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

         /**
          * Filters the trimmed excerpt string.
          *
          * @since 2.8.0
          *
          * @param string $text        The trimmed text.
          * @param string $raw_excerpt The text prior to trimming.
          */
         return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40482>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list