Make WordPress Core

Opened 6 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's profile mattkeys Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Comments Keywords: has-patch needs-testing dev-feedback
Focuses: Cc:


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)

43792.diff (1019 bytes) - added by mattkeys 6 years ago.

Download all attachments as: .zip

Change History (10)

6 years ago

#1 @mattkeys
6 years ago

  • Keywords has-patch needs-testing dev-feedback added

#2 @joyously
6 years ago

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?

#3 @mattkeys
6 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 @joyously
6 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 @mattkeys
6 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.

*update* additionally, looking at the current list of arguments for 'get_comment_excerpt', it originally only had a single argument, then later $comment_id and $comment were added in v4.1.0

Last edited 6 years ago by mattkeys (previous) (diff)

#6 @joyously
6 years ago

I'm sorry, I was mistaken about the parameters. I was thinking of it backwards.

This ticket was mentioned in Slack in #core by mattkeys. View the logs.

6 years ago

#8 @birgire
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:

#9 @pento
6 years ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.