Simple Membership Plugin › Forums › Simple Membership Plugin › Protected message added to the_excerpt()
- This topic has 4 replies, 3 voices, and was last updated 4 years, 6 months ago by
Eldon McGuinness.
-
AuthorPosts
-
January 21, 2022 at 7:42 pm #23505
Eldon McGuinness
ParticipantI think I’m missing something, or something is not working the way one would think. On a particular page I’m trying to show/use the_excerpt for posts and when I do the content of the_excerpt seems to have the, “You need to login to view the rest of the content. Please Login. Not a Member? Join Us” added to the excerpt instead of just showing the excerpt.
I’ve tried to toggle the “Enable More Tag Protection” to no avail. I would think the way this would work is the_excerpt() would be able to return the actual excerpt without the message above appended, but that is not the case.
Am I missing something?
As a hacky fix I’ve added a filter to the_excerpt to strip out the appended message, but this seems like there should be an option to just turn off the appending of the message when one is using the_excerpt()
January 25, 2022 at 9:07 pm #23520mbrsolution
ModeratorHi, our does not protect the excerpt. This plugin is primarily used for protecting the main content of the page (not the excerpt). The excerpt output is very dependent on the theme (different themes do it differently). You may want to try testing one of WordPress default themes like Twenty Twenty.
Kind regards.
January 26, 2022 at 3:30 pm #23525Eldon McGuinness
ParticipantI have and see the same results, here is what looks like is happening when one tries to use the <!–more–> tag to create excerpts, which should work as expected. Use the following for a post body and set it up to be protected with SWPM:
Hi there <!--more--> Body goes hereWith SWPM enabled, one will see the excerpt of, “Hi there”, with the following message attached to it: “You need to login to view the rest of the content. Please Login. Not a Member? Join Us”.
Note: if I disable excerpt protection in SWPM I see the message “You need to login to view this content. Please Login. Not a member? Join Us.”
If I disable SWPM completely and look at the test post I do indeed see what I would expect, which is just “Hi there”. It appears that your code is hijacking the_content by filtering it to protect the content, as of version 4.0.9 you can find this on line number 187 in the function “check_and_apply_more_tag_protection()” of the class.swpm-access-control.php file.
Now the issue lies in how WordPress handles excerpts, in the case that the excerpt box is not used and instead a <!–more–> tag is used, your plugin is preventing just the excerpt from being returned as it is derived from the_content via the wp_trim_excerpt command.
The only solution I can see would be to create a custom function to get the raw post data and do the parsing as needed. However, since this is a departure from how it is expected to run, it might be prudent to include a function that people can call to get the actual excerpt if needed and perhaps keep it in sync with how WordPress handles it. I suppose the plugin could check to see if the page being shown is_home and not append the message, or have an option for something like that, but not sure if it would work out.
In my case, I’m using the following to get a raw excerpt, keep in mind that I’m not using the actual excerpt box in the post so this does not check for that.
// get the excerpt, needed due to how SWPM handles the excerpt function holr_the_excerpt( $length = 55, $suffix = '…' ){ $p = get_post( get_the_ID() ); $excerpt = ( explode( '<!--more-->', $p->post_content ) )[0]; if ( $length < strlen( $excerpt ) ){ $excerpt = substr( $excerpt, 0, $length ); $excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) ); $excerpt .= $suffix; } echo $excerpt; }January 27, 2022 at 8:10 am #23532admin
KeymasterI think you are referring to the “More tag” protection. The “more tag” and “the excerpt” are different things.
We actually don’t handle “the_excerpt” in this plugin. “the_excerpt” seems to have different behavior on different themes and then on different template pages, it can behave differently also. I wasn’t sure how best to provide a consistent experience with that.
Can the following hook be used to override the output from excerpt and just tell it to output whatever you have in the excerpt?
https://developer.wordpress.org/reference/hooks/the_excerpt/Maybe that can be an option in the plugin then.
The “more tag” can also be inconsistent in certain circumstances. Better to use the following partial protection option maybe:
https://simple-membership-plugin.com/apply-partial-section-protection/January 27, 2022 at 4:11 pm #23534Eldon McGuinness
ParticipantI am indeed referring to the More tag protection in a sense, but more so how SWPM handles the_content, which in turn affects how WP handles excerpt when they are derived from the <!–more–> tag. Since SWPM adds a filter to the_content, this same filter gets applied to the_excerpt as the_excerpt actually gets its data from the_content; a pain I know, but that is how it works when using the <!–more–> tag.
The above being said, when you use SWPM and you are in the query loop, hoping to show just the excerpt, you instead get the the_excerpt with the appended message that comes from your more tag protection. Now to be clear, turning off more tag protection is not a solution as that in turn causes the the_excerpt to be ONLY the tag protection message.
Adding a filter to the_excerpt or get_the_excerpt would work the same as what I mentioned above, unless I’m missing something.
function holr_get_the_excerpt() { $length = 55; $suffix = '…'; $p = get_post( get_the_ID() ); $excerpt = strip_shortcodes( $p->post_content ); $excerpt = excerpt_remove_blocks( $excerpt ); $excerpt = strip_tags( $excerpt ); if ( $length < strlen( $excerpt ) ){ $excerpt = substr( $excerpt, 0, $length ); $excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) ); $excerpt .= $suffix; } return $excerpt; } add_filter( "get_the_excerpt", "holr_get_the_excerpt" ); function holr_the_excerpt() { return holr_get_the_excerpt(); } add_filter( "the_excerpt", "holr_the_excerpt" );I updated the above to use the same type of content filtering that the actual WP functions do to generate an excerpt. I’m thinking, in the end, the best solution here is going to be to use something like the above, paired with a check to see what page is being viewed. Then, if the page is matched the excerpt would show up without the message.
-
AuthorPosts
- You must be logged in to reply to this topic.