You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When the function get_the_excerpt() is called, and no manual excerpt exists, wp_trim_excerpt() generates an excerpt from the post content.
The issue is that when the beginning of the post contains a Gutenberg block, it will be rendered. But only the textual content will survive, as wp_strip_all_tags() is called on the generated output.
Excerpts are supposed to be text only, and Gutenberg should not break this convention.
To Reproduce
Create a post, and insert a Gutenberg block outputting a mixture HTML tags and text at the top.
Save the post.
View the generated excerpt in any theme using get_the_excerpt().
Expected behavior
Gutenberg blocks should not be rendered in excerpts.
The way I see it, a block and a shortcode are very similar, so they should be treated the same: being stripped.
Might have to put an exception in for certain blocks for this to work properly. Paragraphs, list, blockquotes, etc. would all be stripped if you just blindly stripped all blocks.
@chrisvanpatten Sure, if you'd like. I'll close otherwise. I need more detail about how to reproduce this issue and what the expected results are. I started looking into it 12 days ago but got stuck when I realized I didn't fully understand the nature of the request.
@danielbachhuber I think it's just fundamentally a design choice (not in the visual sense, but in the broader "how should Gutenberg work" sense).
If you don't pass an explicit excerpt, wp_trim_excerpt() (which is ultimately used by get_the_excerpt() and the_excerpt()) generates an excerpt.
As part of this process, a few things happen.
The raw content is pulled via get_the_content()
The content passes through strip_shortcodes()
The the_content filter is run (critically, this causes Gutenberg blocks to be rendered via the do_blocks function)
The "rendered" content passes throughwp_trim_words()…
…which itself runswp_strip_all_tags(), stripping all HTML tags but preserving text contained within them
The resulting excerpt is filtered again (via a wp_trim_excerpt filter) and returned.
This means that blocks, or at least the text from all blocks, are included in an auto-generated excerpt. Image gallery captions, text from a cover image block, any text from server-rendered blocks (like post names/authors/etc in the Latest Posts block), possibly text from certain embeds, etc. are all potentially included in this output. These are pieces of content that would probably be included via shortcode in a pre-Gutenberg world, and thus would not have been part of the excerpt (because of the strip_shortcodes() call).
Ultimately I think the question is: should there be a strip_blocks() happening alongside (or eventually in place of) that strip_shortcodes() call, to remove all that "text-you-probably-don't-want-in-an-excerpt" content?
I think that's a reasonable request but also adds an additional level of complexity because stripping all blocks also means the text you would probably want included an excerpt (e.g. paragraphs, list items, blockquotes, etc.) would be stripped, since now everything is a block (vs. before, where you had "shortcodes" and "everything else").
So perhaps strip_blocks() would need to work more selectively, sort of like a blend between strip_shortcodes() and wp_kses(), e.g.…
…where the second parameter is a list of blocks that will be preserved. In the case of wp_trim_excerpt() that second parameter could be filtered in case block authors have "textual" blocks (we have a custom "section header" block on a project, for instance) that they want included in generated excerpts.
Anyhow, this got much longer than I expected so I'll stop here. Would love to hear thoughts from the team :)
The Gutenberg implementation involves some hacky code that adds strip_dynamic_blocks to the the_content filter then removes it again when wp_trim_excerpt is run - the function invokes the filter hook.
This assumes that wp_trim_excerpt will be run.
In one of my plugins I had replaced wp_trim_excerpt with my own code.
Since wp_trim_excerpt is no longer being run the strip_dynamic_blocks hook is not being removed. So my dynamic blocks did not appear when the post was being displayed singly and WordPress SEO is activated.
Activity
danielbachhuber commentedon Jun 14, 2018
I agree with this assessment.
chrisvanpatten commentedon Jun 14, 2018
Might have to put an exception in for certain blocks for this to work properly. Paragraphs, list, blockquotes, etc. would all be stripped if you just blindly stripped all blocks.
danielbachhuber commentedon Jun 14, 2018
@fklein-lu Can you include more detail of your expected behavior?
danielbachhuber commentedon Jun 26, 2018
@chrisvanpatten Do you understand this issue well-enough to include full reproduction steps and detail of expected behavior?
chrisvanpatten commentedon Jun 26, 2018
@danielbachhuber I'm not sure what you mean — I didn't report the issue. Want me to take it over? Happy to help, just not sure what you need :)
danielbachhuber commentedon Jun 26, 2018
@chrisvanpatten Sure, if you'd like. I'll close otherwise. I need more detail about how to reproduce this issue and what the expected results are. I started looking into it 12 days ago but got stuck when I realized I didn't fully understand the nature of the request.
chrisvanpatten commentedon Jun 26, 2018
@danielbachhuber I think it's just fundamentally a design choice (not in the visual sense, but in the broader "how should Gutenberg work" sense).
If you don't pass an explicit excerpt,
wp_trim_excerpt()(which is ultimately used byget_the_excerpt()andthe_excerpt()) generates an excerpt.As part of this process, a few things happen.
get_the_content()strip_shortcodes()the_contentfilter is run (critically, this causes Gutenberg blocks to be rendered via thedo_blocksfunction)wp_trim_words()…wp_strip_all_tags(), stripping all HTML tags but preserving text contained within themwp_trim_excerptfilter) and returned.This means that blocks, or at least the text from all blocks, are included in an auto-generated excerpt. Image gallery captions, text from a cover image block, any text from server-rendered blocks (like post names/authors/etc in the Latest Posts block), possibly text from certain embeds, etc. are all potentially included in this output. These are pieces of content that would probably be included via shortcode in a pre-Gutenberg world, and thus would not have been part of the excerpt (because of the
strip_shortcodes()call).Ultimately I think the question is: should there be a
strip_blocks()happening alongside (or eventually in place of) thatstrip_shortcodes()call, to remove all that "text-you-probably-don't-want-in-an-excerpt" content?I think that's a reasonable request but also adds an additional level of complexity because stripping all blocks also means the text you would probably want included an excerpt (e.g. paragraphs, list items, blockquotes, etc.) would be stripped, since now everything is a block (vs. before, where you had "shortcodes" and "everything else").
So perhaps
strip_blocks()would need to work more selectively, sort of like a blend betweenstrip_shortcodes()andwp_kses(), e.g.……where the second parameter is a list of blocks that will be preserved. In the case of
wp_trim_excerpt()that second parameter could be filtered in case block authors have "textual" blocks (we have a custom "section header" block on a project, for instance) that they want included in generated excerpts.Anyhow, this got much longer than I expected so I'll stop here. Would love to hear thoughts from the team :)
danielbachhuber commentedon Jun 27, 2018
Thanks @chrisvanpatten, this is exactly the clarification I was looking for.
obenland commentedon Aug 13, 2018
How about only stripping dynamic blocks? Keeping blocks that save their markup in post content should maintain the current behavior, no?
Introduce strip_dynamic_blocks() for excerpts (#8984)
bobbingwide commentedon Nov 27, 2018
The Gutenberg implementation involves some hacky code that adds
strip_dynamic_blocksto thethe_contentfilter then removes it again whenwp_trim_excerptis run - the function invokes the filter hook.This assumes that
wp_trim_excerptwill be run.In one of my plugins I had replaced
wp_trim_excerptwith my own code.Since wp_trim_excerpt is no longer being run the strip_dynamic_blocks hook is not being removed. So my dynamic blocks did not appear when the post was being displayed singly and WordPress SEO is activated.
My workaround is to not remove the filter.