-
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
Post excerpt: Add excerpt length control #44964
Changes from all commits
bef63aa
5098f0d
b7a54a3
17576fa
5059371
aa5db23
34b2691
bc051de
1d686af
86342e2
55edf70
ff3f9ee
5a9178d
ef266d5
daa5b05
e8c557f
085c025
5783b0f
7771d02
0ddaf98
c32920f
64d255c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,18 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) { | |
return ''; | ||
} | ||
|
||
$excerpt = get_the_excerpt(); | ||
|
||
if ( empty( $excerpt ) ) { | ||
return ''; | ||
/* | ||
* The purpose of the excerpt length setting is to limit the length of both | ||
* automatically generated and user-created excerpts. | ||
* Because the excerpt_length filter only applies to auto generated excerpts, | ||
* wp_trim_words is used instead. | ||
*/ | ||
$excerpt_length = $attributes['excerptLength']; | ||
if ( isset( $excerpt_length ) ) { | ||
$excerpt = wp_trim_words( get_the_excerpt(), $excerpt_length ); | ||
} else { | ||
$excerpt = get_the_excerpt(); | ||
Comment on lines
+27
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not inject a filter with the dynamic excerpt length and just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe I tried that during the development, so there is no obvious reason for not trying that as a possible improvement in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The addition of the call to "wp_trim_words" will remove any HTML that users have put into a custom excerpt by over-riding get_the_excerpt. Was this deliberate, or an unfortunate by-product? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe that was accidental, so thank you for the flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Workaround to bypass the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you - that's a really helpful workaround for now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was also reported in the forums and I've confirmed it was introduced in WordPress 6.3. Created an issue at #66195 |
||
} | ||
|
||
$more_text = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . wp_kses_post( $attributes['moreText'] ) . '</a>' : ''; | ||
$filter_excerpt_more = function( $more ) use ( $more_text ) { | ||
return empty( $more_text ) ? $more : ''; | ||
|
@@ -70,3 +76,21 @@ function register_block_core_post_excerpt() { | |
); | ||
} | ||
add_action( 'init', 'register_block_core_post_excerpt' ); | ||
|
||
/** | ||
* If themes or plugins filter the excerpt_length, we need to | ||
* override the filter in the editor, otherwise | ||
* the excerpt length block setting has no effect. | ||
* Returns 100 because 100 is the max length in the setting. | ||
*/ | ||
if ( is_admin() || | ||
defined( 'REST_REQUEST' ) || | ||
'REST_REQUEST' ) { | ||
Comment on lines
+86
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very wide net to cast which will affect all instances of excerpt filtering, regardless of source (Post Excerpt block, Latest Post block, traditional theme templates, etc.) — see #48403. Why not surround the rendering logic in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant by my suggestion was something like this (untested): function render_block_core_post_excerpt( $attributes, $content, $block ) {
global $_block_core_post_excerpt_length;
// ...
$_block_core_post_excerpt_length = int( $attributes['excerptLength'] );
add_filter( 'excerpt_length', '_block_core_post_excerpt_filter', PHP_INT_MAX );
get_the_excerpt();
remove_filter( 'excerpt_length', '_block_core_post_excerpt_filter' );
// ... with function _block_core_post_excerpt_filter() {
global $_block_core_post_excerpt_length;
return $_block_core_post_excerpt_length;
} The whole There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a pull request open that tries to address this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Which? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it's the one @carolinan was referring to, but I opened #48598 when I discovered that my excerpts outside of the block editor were not obeying my own excerpt_length filter and traced it back to the line you referenced here:
My last revision in my PR did something like what you suggest here:
But it only applies to the rendered block on the page; it is not reflected in the editor. The condition on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is the PR I was thinking of |
||
add_filter( | ||
'excerpt_length', | ||
function() { | ||
return 100; | ||
}, | ||
PHP_INT_MAX | ||
); | ||
} |
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 tried using
&hellip
; but it did not work. In the block editor the characters flashed before they were converted and in the site editor only the plain characters showed.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.
Any reason we shouldn't use the
…
directly?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'll ask the same thing Rich asked.
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.
No reason, I only missed the question the first time :)