-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds the_excerpt
filter in the core/post-excerpt
block.
#67774
base: trunk
Are you sure you want to change the base?
Adds the_excerpt
filter in the core/post-excerpt
block.
#67774
Conversation
Description: This allows us to have consistency between frontend and backend.
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @CoderAbhinav! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
@@ -27,7 +27,15 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) { | |||
* wp_trim_words is used instead. | |||
*/ | |||
$excerpt_length = $attributes['excerptLength']; | |||
$excerpt = get_the_excerpt( $block->context['postId'] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function already has a filter - get_the_excerpt
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it. The issue concerns the PHP filter. There's no need to update the JS code. For various reasons, the blocks don't mirror PHP filters in their React components.
P.S. I would recommend using reserved GitHub keywords to reference issues. It makes clear what you're trying to solve.
Thanks for contributing, @CoderAbhinav! What is PR is trying to solve? The PHP rendered has the correct filters, and data on the client side is consumed via REST API, which also has excerpt filters. There's no need to mimic PHP filters in client-side code (JS), as they serve a different purpose, and filters in JS can negatively affect performance. |
@@ -27,7 +27,8 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) { | |||
* wp_trim_words is used instead. | |||
*/ | |||
$excerpt_length = $attributes['excerptLength']; | |||
$excerpt = get_the_excerpt( $block->context['postId'] ); | |||
|
|||
$excerpt = apply_filters( 'the_excerpt', get_the_excerpt( $block->context['postId'] ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CoderAbhinav I think this will result in all the default filters being applied, so it'd be good to make everything still works correctly:
https://github.com/WordPress/wordpress-develop/blob/6106332617128820c0ee5132c34ff1cf6f785d1a/src/wp-includes/default-filters.php#L205-L211
I did some testing personally and couldn't see any issues. I think most of these filters like convert_smilies
are already run on the content, so there's no change from trunk to this PR.
The change might also need a dev note, as some plugins might be working around the missing filter and reintroducing it could require plugins updating their code.
As long as everything works, then this change seems fine (apart from Mamaduka's request change). There's precedence in that the post content block also does this and has done for a long time:
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); |
What?
This PR adds missing
the_excerpt
filter. Allowing much more consistent usage.Why?
The post excerpt block does not call the the_excerpt filter before rendering the block. In contrast, the post content block does call the the_content filter before rendering. (Ref. #28214). In order to add consistency this filter has been added.
How?
This PR adds filter in frontend (
index.js
) as well as in theedit.js
of the block.https://github.com/CoderAbhinav/gutenberg/blob/351c8fe74313d3f2f8eabbbcca1ac91224751347/packages/block-library/src/post-excerpt/index.php#L31-L38
https://github.com/CoderAbhinav/gutenberg/blob/756b5970d155feae8a45f23fd62473dacd6a89b6/packages/block-library/src/post-excerpt/edit.js#L149-L155
The implementation is done with reference to https://developer.wordpress.org/reference/functions/the_excerpt/#source this code.
Testing Instructions
Add a filter like this first.