Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#35308 new enhancement

get_the_excerpt: For protected posts the output should be able to be filtered

Reported by: emanaku Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:
PR Number:


If I want to show an excerpt of a protected post, then I have to search the output of get_the_excerpt for a string like "There is no excerpt because this is a protected post." and replace this string by the actual excerpt.
In a multilanguage environment this creates a source of possible errors.
I would like to see that the filter "get_the_excerpt" is also applied to the "There is no excerpt ..." message.

Code in the function get_the_excerpt could look like this:

function get_the_excerpt( $deprecated = '' ) {
        if ( !empty( $deprecated ) )
                _deprecated_argument( __FUNCTION__, '2.3' );

        $post = get_post();
        if ( empty( $post ) ) {
                return '';
        $excerpt = '';                                                                                                  // emanaku new  
        if ( post_password_required() ) {
                // return __( 'There is no excerpt because this is a protected post.' ); // emanaku replace with next line
                $excerpt = __( 'There is no excerpt because this is a protected post.' ); // emanaku new
        else {  $excerpt = $post->post_excerpt; };                                              // emanaku new
         * Filter the retrieved post excerpt.
         * @since 1.2.0
         * @param string $post_excerpt The post excerpt.
        // return apply_filters( 'get_the_excerpt', $post->post_excerpt );      // emanaku replace with next line
        return apply_filters( 'get_the_excerpt', $excerpt ); // emanaku changed

With this change a filter asking for protected content can hook into get_the_excerpt and can deliver any kind of excerpt (the real excerpt, a customized protected text, a form to give the password etc.)

Attachments (1)

35308-protected-excerpt.diff (580 bytes) - added by Tmeister 4 years ago.
New filter added to the string.

Download all attachments as: .zip

Change History (4)

#1 @swissspidy
4 years ago

  • Version 4.3.1 deleted

Adding the same filter sounds like a bad idea. Depending on what plugins do, readers could suddenly see the excerpt of posts they do not have access too.

So this would need to be a new filter. However, you can already modify the There is no excerpt because this is a protected post. message using the gettext filter.

4 years ago

New filter added to the string.

#2 @Tmeister
4 years ago

  • Keywords has-patch added

I add a new filter to the string.

Maybe is not the appropriate filter name, but it can be changed, of course.

#3 @emanaku
4 years ago

Thank you, Tmeister, for providing the diff file - way easier than my proposal ;-) And I agree with the idea of a separate filter.
The gettext solution, swissspidy, is not feasable, because the get_the_excerpt can be used by many plugins and themes, and I do not want to maintain such a mess ;-)
Thank you both!

Note: See TracTickets for help on using tickets.