Opened 7 years ago
Last modified 6 years ago
#43792 new enhancement
get_comment_excerpt filter should tell if the comment was shorted or not
Reported by: | mattkeys | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Comments | Keywords: | has-patch needs-testing dev-feedback |
Focuses: | Cc: |
Description
The design I am currently working on has long comments shortened to 100 words, and a 'show more' / 'show less' link is added.
I only want to add the 'show more' text if the comment has been shortened. There is a filter before get_comment_excerpt() returns that I can use to add my link. However it does not pass along information about whether or not the comment was shortened.
As a workaround I can check if the comment ends in "…", but it would make sense to add a boolean value to the filter arguments to make this easier/cleaner.
Attachments (1)
Change History (10)
#3
@
7 years ago
Hi @joyously thanks for looking at this. $use_ellipsis is a boolean already set by the get_comment_excerpt() function that is used to determine if the comment needs to be shortened, I am just passing its value to the filter at the end of the function.
There are workarounds; I mentioned checking for the … at the end of the string as one possible workaround. Your idea of checking both string lengths could work as well. In my opinion both of these ideas are a bit cumbersome, especially if we already have a boolean value at our disposal, we just need to add it to the filter.
#4
@
7 years ago
I understand what you're saying, but adding a parameter means that all the code using the filter will get an error since it will have the wrong number of parameters. So it's not a trivial thing to do, and you should work with the parameters that you already have if you can.
#5
@
7 years ago
I don't believe that to be the case, but would be interested to hear more if you are able to reproduce what you are saying.
The argument I added is at the 'end' of the current list of arguments. Anyone who is currently using the filter like so:
add_filter( 'get_comment_excerpt', 'my_comment_filter, 10, 3 );
Will continue to get the first 3 arguments that their add_filter() call specifies. If they want to get the new 4th argument, they would need to update their code to be:
add_filter( 'get_comment_excerpt', 'my_comment_filter, 10, 4 );
I've tested both variations locally with no errors. Let me know if I am missing something.
This ticket was mentioned in Slack in #core by mattkeys. View the logs.
6 years ago
#8
@
6 years ago
Related #44541 where it's suggested to use wp_trim_words()
within get_comment_excerpt()
.
I wonder if this is the way forward, if the same should apply for post excerpts as well, through the wp_trim_words
filter:
https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/formatting.php#L3334
Wouldn't the boolean you added need to be set to something?
And isn't the excerpt and the comment object passed already? So you can just compare the lengths?