wppaste
WordPress

apply_block_hooks_to_content( string $content, WP_Block_Template|WP_Post|array|null $context = null, callable $callback = 'insert_hooked_blocks' ): string

Since
6.6.0, 6.7.0, 6.8.0
Source
wp-includes/blocks.php:1162
Runs the hooked blocks algorithm on the given content.

Compatibility

WordPress
since 6.8.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
Serialized content.
$contextWP_Block_Template|WP_Post|array|nulloptional
A block template, template part, post object, or pattern that the blocks belong to. If set to null, get_post() will be called to use the current post as context.
Default: null.Default: null
$callbackcallableoptional
A function that will be called for each block to generate the markup for a given list of blocks that are hooked to it.
Default: 'insert_hooked_blocks'.Default: 'insert_hooked_blocks'

Return value

string
The serialized markup.

Performance profile

How much work a call to apply_block_hooks_to_content() 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
7–8

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
9

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

What it touches

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

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

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

WhenInstructionsCalls it makes
always7–8none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1417–819
8.51417–819
8.41417–8193 fewer instructions than PHP 8.3
8.31447–819
8.21447–819
8.11447–819
7.41447–819

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.

Uses · 13

  • get_post()Retrieves post data given a post ID or post object.
  • get_hooked_blocks()Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
  • has_filter()Checks if any filter has been registered for a hook.
  • make_before_block_visitor()Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
  • make_after_block_visitor()Returns a function that injects the hooked blocks after a given block.
  • block_has_support()Checks whether the current block type supports the feature requested.
  • has_block()Determines whether a $post or a string contains a specific block type.
  • add_filter()Adds a callback function to a filter hook.
  • traverse_and_serialize_blocks()Given an array of parsed block trees, applies callbacks before and after serializing them and returns their concatenated output.
  • parse_blocks()Parses blocks out of a content string.
  • remove_filter()Removes a callback function from a filter hook.
  • WP_Block_Type_Registry::get_instance()::get_registered()
Show all 13

Used by · 9

Source code

function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) {	// Default to the current post if no context is provided.	if ( null === $context ) {		$context = get_post();	} 	$hooked_blocks = get_hooked_blocks(); 	$before_block_visitor = '_inject_theme_attribute_in_template_part_block';	$after_block_visitor  = null;	if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {		$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );		$after_block_visitor  = make_after_block_visitor( $hooked_blocks, $context, $callback );	} 	$block_allows_multiple_instances = array();	/*	 * Remove hooked blocks from `$hooked_block_types` if they have `multiple` set to false and	 * are already present in `$content`.	 */	foreach ( $hooked_blocks as $anchor_block_type => $relative_positions ) {		foreach ( $relative_positions as $relative_position => $hooked_block_types ) {			foreach ( $hooked_block_types as $index => $hooked_block_type ) {				$hooked_block_type_definition =					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type ); 				$block_allows_multiple_instances[ $hooked_block_type ] =					block_has_support( $hooked_block_type_definition, 'multiple', true ); 				if (					! $block_allows_multiple_instances[ $hooked_block_type ] &&					has_block( $hooked_block_type, $content )				) {					unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ][ $index ] );				}			}			if ( empty( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) ) {				unset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] );			}		}		if ( empty( $hooked_blocks[ $anchor_block_type ] ) ) {			unset( $hooked_blocks[ $anchor_block_type ] );		}	} 	/*	 * We also need to cover the case where the hooked block is not present in	 * `$content` at first and we're allowed to insert it once -- but not again.	 */	$suppress_single_instance_blocks = static function ( $hooked_block_types ) use ( &$block_allows_multiple_instances, $content ) {		static $single_instance_blocks_present_in_content = array();		foreach ( $hooked_block_types as $index => $hooked_block_type ) {			if ( ! isset( $block_allows_multiple_instances[ $hooked_block_type ] ) ) {				$hooked_block_type_definition =					WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type ); 				$block_allows_multiple_instances[ $hooked_block_type ] =					block_has_support( $hooked_block_type_definition, 'multiple', true );			} 			if ( $block_allows_multiple_instances[ $hooked_block_type ] ) {				continue;			} 			// The block doesn't allow multiple instances, so we need to check if it's already present.			if (				in_array( $hooked_block_type, $single_instance_blocks_present_in_content, true ) ||				has_block( $hooked_block_type, $content )			) {				unset( $hooked_block_types[ $index ] );			} else {				// We can insert the block once, but need to remember not to insert it again.				$single_instance_blocks_present_in_content[] = $hooked_block_type;			}		}		return $hooked_block_types;	};	add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );	$content = traverse_and_serialize_blocks(		parse_blocks( $content ),

Changelog

Introduced in 6.6.0. One change between 6.7.7 and 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.

6.8.8
Parameter $context retyped from WP_Block_Template|WP_Post|array to WP_Block_Template|WP_Post|array|null.verified against source
6.8.0
Have the $context parameter default to null, in which case get_post() will be called to use the current post as context.from the docblock
6.7.0
Injects the theme attribute into Template Part blocks, even if no hooked blocks are registered.from the docblock
6.6.0
Introduced.from the docblock

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.