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 | 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)
Change History (6)
#2
@
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
@
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!
#5
@
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).
Our use case was a little different in that we wanted to filter the_excerpt in a password protected post to add some custom code - no dice!
@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.
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 thegettext
filter.