wppaste
WordPress

excerpt_remove_blocks( string $content ): string

Since
5.0.0
Source
wp-includes/blocks.php:2108
Parses blocks out of a content string, and renders those appropriate for the excerpt.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–30

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.

Plugin surface
2 hooks

Third-party callbacks on 'excerpt_allowed_wrapper_blocks', 'excerpt_allowed_blocks' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_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.

WhenInstructionsCalls it makes
!has_blocks()6has_blocks()
has_blocks()29–30has_blocks(), apply_filters(), array_merge(), apply_filters(), parse_blocks()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev646–3010
8.5646–3010
8.4646–30109 fewer instructions than PHP 8.3
8.3736–3010
8.2736–3010
8.1736–3010
7.4736–3010

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:

  1. apply_filters( excerpt_allowed_wrapper_blocks )filterline 2143 (+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.

  2. apply_filters( excerpt_allowed_blocks )filterline 2157 (+49 into the body)

    Filters the list of blocks that can contribute to the excerpt.

Uses · 5

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.