wppaste
WordPress

twenty_twenty_one_print_first_instance_of_block( string $block_name, string|null $content = null, int $instances = 1 ): bool

Since
Twenty Twenty-One 1.0
Source
wp-content/themes/twentytwentyone/inc/template-functions.php:368
Prints the first instance of a block in the content, and then break away.

Compatibility

WordPress
since Twenty Twenty-One 1.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

$block_namestring
The full block type name, or a partial match.
Example: core/image, core-embed/*.
$contentstring|nulloptional
The content to search in. Use null for get_the_content().Default: null
$instancesintoptional
How many instances of the block will be printed (max). Default 1.Default: 1

Return value

bool
Returns true if a block was located & printed, otherwise false.

Performance profile

How much work a call to twenty_twenty_one_print_first_instance_of_block() 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
14–45

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

Plugin surface
1 hook

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below twenty_twenty_one_print_first_instance_of_block()

Further down the call graph this can also reach option, transient, cache and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost twenty_twenty_one_print_first_instance_of_block() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–15parse_blocks()
always17–18get_the_content(), parse_blocks()
always19–20parse_blocks(), apply_filters()
always22–23get_the_content(), parse_blocks(), apply_filters()
!$blocks && isset($block) && $instances31parse_blocks(), render_block()
!$blocks && isset($block) && $instances34get_the_content(), parse_blocks(), render_block()
!$blocks && isset($block) && $instances36parse_blocks(), render_block(), apply_filters()
!$blocks && isset($block) && $instances37parse_blocks(), rtrim(), render_block()
!$blocks && isset($block) && $instances39get_the_content(), parse_blocks(), render_block(), apply_filters()
!$blocks && isset($block) && $instances40get_the_content(), parse_blocks(), rtrim(), render_block()
!$blocks && isset($block) && $instances42parse_blocks(), rtrim(), render_block(), apply_filters()
!$blocks && isset($block) && $instances45get_the_content(), parse_blocks(), rtrim(), render_block(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4914–458
8.54914–458
8.44914–4583 fewer instructions than PHP 8.3
8.35214–488
8.25214–488
8.15214–488
7.45214–488

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 · 1

One hook fires while twenty_twenty_one_print_first_instance_of_block() runs, in this order:

  1. apply_filters( the_content )filterline 413 (+45 into the body)

    Filters the post content.

Uses · 4

Source code

function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) {	$instances_count = 0;	$blocks_content  = ''; 	if ( ! $content ) {		$content = get_the_content();	} 	// Parse blocks in the content.	$blocks = parse_blocks( $content ); 	// Loop blocks.	foreach ( $blocks as $block ) { 		// Confidence check.		if ( ! isset( $block['blockName'] ) ) {			continue;		} 		// Check if this the block matches the $block_name.		$is_matching_block = false; 		// If the block ends with *, try to match the first portion.		if ( '*' === $block_name[-1] ) {			$is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) );		} else {			$is_matching_block = $block_name === $block['blockName'];		} 		if ( $is_matching_block ) {			// Increment count.			++$instances_count; 			// Add the block HTML.			$blocks_content .= render_block( $block ); 			// Break the loop if the $instances count was reached.			if ( $instances_count >= $instances ) {				break;			}		}	} 	if ( $blocks_content ) {		/** This filter is documented in wp-includes/post-template.php */		echo apply_filters( 'the_content', $blocks_content );		return true;	} 	return false;}

Changelog

Introduced in Twenty Twenty-One 1.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 7.1.0 tag, from src/wp-content/themes/twentytwentyone/inc/template-functions.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.