excerpt_remove_blocks( string $content ): string
- Since
- 5.0.0
- Source
wp-includes/blocks.php:2187
Description
As the excerpt should be a small string of text relevant to the full post content, this function renders the blocks that are most likely to contain such text.
Compatibility
- WordPress
- since 5.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$contentstring- The content to parse.
Return value
string- The parsed and filtered content.
Performance profile
How much work a call to excerpt_remove_blocks() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 6–30
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Third-party callbacks on 'excerpt_allowed_wrapper_blocks', 'excerpt_allowed_blocks' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below excerpt_remove_blocks()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost excerpt_remove_blocks() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!has_blocks() | 6 | has_blocks() |
has_blocks() | 29–30 | has_blocks(), apply_filters(), array_merge(), apply_filters(), parse_blocks() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 64 | 6–30 | 10 | |
| 8.5 | 64 | 6–30 | 10 | |
| 8.4 | 64 | 6–30 | 10 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 73 | 6–30 | 10 | |
| 8.2 | 73 | 6–30 | 10 | |
| 8.1 | 73 | 6–30 | 10 | |
| 7.4 | 73 | 6–30 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while excerpt_remove_blocks() runs, in this order:
- apply_filters( excerpt_allowed_wrapper_blocks )filterline 2222 (+35 into the body)
Filters the list of blocks that can be used as wrapper blocks, allowing excerpts to be generated from the `innerBlocks` of these wrappers.
- apply_filters( excerpt_allowed_blocks )filterline 2236 (+49 into the body)
Filters the list of blocks that can contribute to the excerpt.
Uses · 5
- has_blocks()Determines whether a post or content string has blocks.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- parse_blocks()Parses blocks out of a content string.
- _excerpt_render_inner_blocks()Renders inner blocks from the allowed wrapper blocks for generating an excerpt.
- render_block()Renders a single block into a HTML string.
Used by · 1
- wp_trim_excerpt()Generates an excerpt from the content, if needed.
Source code
function excerpt_remove_blocks( $content ) { if ( ! has_blocks( $content ) ) { return $content; } $allowed_inner_blocks = array( // Classic blocks have their blockName set to null. null, 'core/freeform', 'core/heading', 'core/html', 'core/list', 'core/media-text', 'core/paragraph', 'core/preformatted', 'core/pullquote', 'core/quote', 'core/table', 'core/verse', ); $allowed_wrapper_blocks = array( 'core/columns', 'core/column', 'core/group', ); /** * Filters the list of blocks that can be used as wrapper blocks, allowing * excerpts to be generated from the `innerBlocks` of these wrappers. * * @since 5.8.0 * * @param string[] $allowed_wrapper_blocks The list of names of allowed wrapper blocks. */ $allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks ); $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks ); /** * Filters the list of blocks that can contribute to the excerpt. * * If a dynamic block is added to this list, it must not generate another * excerpt, as this will cause an infinite loop to occur. * * @since 5.0.0 * * @param string[] $allowed_blocks The list of names of allowed blocks. */ $allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks ); $blocks = parse_blocks( $content ); $output = ''; foreach ( $blocks as $block ) { if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { if ( ! empty( $block['innerBlocks'] ) ) { if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) { $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks ); continue; } // Skip the block if it has disallowed or nested inner blocks. foreach ( $block['innerBlocks'] as $inner_block ) { if ( ! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) || ! empty( $inner_block['innerBlocks'] ) ) { continue 2; } } } $output .= render_block( $block ); } } return $output;}Changelog
Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/blocks.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.