Make WordPress Core

Opened 9 years ago

Last modified 4 years ago

#35308 new enhancement

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

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

Description

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:

<?php
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 9 years ago.
New filter added to the string.

Download all attachments as: .zip

Change History (6)

#1 @swissspidy
9 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.

@Tmeister
9 years ago

New filter added to the string.

#2 @Tmeister
9 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
9 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!

#4 @dlh
4 years ago

#50079 was marked as a duplicate.

#5 @themightymo
4 years ago

@emanaku Thank you for submitting this ticket! My team and I invested about 10+ dev hours trying to filter the There is no excerpt because this is a protected post. string before finally finding this ticket (we were going to submit our own ticket and glad to hear we're not the first to discover this issue).

@Tmeister @dlh Fwiw, there are instructions in the WP official Support docs that seem to be blocked by this...which makes me think it might be a bug, actually.

Version 3, edited 4 years ago by themightymo (previous) (next) (diff)
Note: See TracTickets for help on using tickets.