wppaste
WordPress

insert_hooked_blocks( array $parsed_anchor_block, string $relative_position, array $hooked_blocks, WP_Block_Template|WP_Post|array $context ): string

Since
6.5.0
Source
wp-includes/blocks.php:1008
Returns the markup for blocks hooked to the given anchor block in a specific relative position.

Compatibility

WordPress
since 6.5.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

$parsed_anchor_blockarray
The anchor block, in parsed block array format.
$relative_positionstring
The relative position of the hooked blocks.
Can be one of 'before', 'after', 'first_child', or 'last_child'.
$hooked_blocksarray
An array of hooked block types, grouped by anchor block and relative position.
$contextWP_Block_Template|WP_Post|array
The block template, template part, or pattern that the anchor block belongs to.

Return value

string

Performance profile

How much work a call to insert_hooked_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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
21–27

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

Plugin surface
3 hooks

Third-party callbacks on 'hooked_block_types', 'hooked_block', 'hooked_block_{$hooked_block_type}' 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

Further down the call graph this can also reach option, cache, serialize, query 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always21–27apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7021–277
8.57021–277
8.47021–2773 fewer instructions than PHP 8.3
8.37321–277
8.27321–277
8.17321–2771 fewer instruction than PHP 7.4
7.47421–287

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

3 hooks fire while insert_hooked_blocks() runs, in this order:

  1. apply_filters( hooked_block_types )filterline 1026 (+18 into the body)

    Filters the list of hooked block types for a given anchor block type and relative position.

  2. apply_filters( hooked_block )filterline 1050 (+42 into the body)

    Filters the parsed block array for a given hooked block.

  3. apply_filters( hooked_block_{$hooked_block_type} )filterline 1066 (+58 into the body)

    Filters the parsed block array for a given hooked block.

Uses · 2

  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • serialize_block()Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.

Used by · 1

Source code

function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {	$anchor_block_type  = $parsed_anchor_block['blockName'];	$hooked_block_types = isset( $anchor_block_type, $hooked_blocks[ $anchor_block_type ][ $relative_position ] )		? $hooked_blocks[ $anchor_block_type ][ $relative_position ]		: array(); 	/**	 * Filters the list of hooked block types for a given anchor block type and relative position.	 *	 * @since 6.4.0	 *	 * @param string[]                        $hooked_block_types The list of hooked block types.	 * @param string                          $relative_position  The relative position of the hooked blocks.	 *                                                            Can be one of 'before', 'after', 'first_child', or 'last_child'.	 * @param string                          $anchor_block_type  The anchor block type.	 * @param WP_Block_Template|WP_Post|array $context            The block template, template part, post object,	 *                                                            or pattern that the anchor block belongs to.	 */	$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); 	$markup = '';	foreach ( $hooked_block_types as $hooked_block_type ) {		$parsed_hooked_block = array(			'blockName'    => $hooked_block_type,			'attrs'        => array(),			'innerBlocks'  => array(),			'innerHTML'    => '',			'innerContent' => array(),		); 		/**		 * Filters the parsed block array for a given hooked block.		 *		 * @since 6.5.0		 *		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.		 * @param string                          $hooked_block_type   The hooked block type name.		 * @param string                          $relative_position   The relative position of the hooked block.		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,		 *                                                             or pattern that the anchor block belongs to.		 */		$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); 		/**		 * Filters the parsed block array for a given hooked block.		 *		 * The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.		 *		 * @since 6.5.0		 *		 * @param array|null                      $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.		 * @param string                          $hooked_block_type   The hooked block type name.		 * @param string                          $relative_position   The relative position of the hooked block.		 * @param array                           $parsed_anchor_block The anchor block, in parsed block array format.		 * @param WP_Block_Template|WP_Post|array $context             The block template, template part, post object,		 *                                                             or pattern that the anchor block belongs to.		 */		$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); 		if ( null === $parsed_hooked_block ) {			continue;		} 		// It's possible that the filter returned a block of a different type, so we explicitly		// look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.		if (			! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||			! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )		) {			$markup .= serialize_block( $parsed_hooked_block );		}	} 	return $markup;}

Changelog

Introduced in 6.5.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-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.