wppaste
WordPress

apply_block_hooks_to_content_from_post_object( string $content, WP_Post|null $post = null, callable $callback = 'insert_hooked_blocks', array|null $ignored_hooked_blocks_at_root = null ): string

Since
6.8.0, 7.0.0
Source
wp-includes/blocks.php:1274
Run the Block Hooks algorithm on a post object's content.

Description

This function is different from apply_block_hooks_to_content in that it takes ignored hooked block information from the post's metadata into account. This ensures that any blocks hooked as first or last child of the block that corresponds to the post type are handled correctly.

Compatibility

WordPress
since 7.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 4 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$contentstring
Serialized content.
$postWP_Post|nulloptional
A post object that the content belongs 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'
$ignored_hooked_blocks_at_rootarray|nulloptional
A reference to an array that will be populated with the ignored hooked blocks at the root level.
Default: null.Default: null

Return value

string
The serialized markup.

Performance profile

How much work a call to apply_block_hooks_to_content_from_post_object() 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
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–9

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 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_from_post_object()

Further down the call graph this can also reach cache, serialize, option 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_from_post_object() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7–9none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1237–911
8.51237–911
8.41237–9113 fewer instructions than PHP 8.3
8.31267–911
8.21267–911
8.11267–911
7.41267–911

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

Used by · 5

Source code

function apply_block_hooks_to_content_from_post_object(	$content,	$post = null,	$callback = 'insert_hooked_blocks',	&$ignored_hooked_blocks_at_root = null) {	// Default to the current post if no context is provided.	if ( null === $post ) {		$post = get_post();	} 	if ( ! $post instanceof WP_Post ) {		return apply_block_hooks_to_content( $content, $post, $callback );	} 	/*	 * If the content was created using the classic editor or using a single Classic block	 * (`core/freeform`), it might not contain any block markup at all.	 * However, we still might need to inject hooked blocks in the first child or last child	 * positions of the parent block. To be able to apply the Block Hooks algorithm, we wrap	 * the content in a `core/freeform` wrapper block.	 */	if ( ! has_blocks( $content ) ) {		$original_content = $content; 		$content_wrapped_in_classic_block = get_comment_delimited_block_content(			'core/freeform',			array(),			$content		); 		$content = $content_wrapped_in_classic_block;	} 	$attributes = array(); 	// If context is a post object, `ignoredHookedBlocks` information is stored in its post meta.	$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );	if ( ! empty( $ignored_hooked_blocks ) ) {		$ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );		$attributes['metadata'] = array(			'ignoredHookedBlocks' => $ignored_hooked_blocks,		);	} 	/*	 * We need to wrap the content in a temporary wrapper block with that metadata	 * so the Block Hooks algorithm can insert blocks that are hooked as first or last child	 * of the wrapper block.	 * To that end, we need to determine the wrapper block type based on the post type.	 */	if ( 'wp_navigation' === $post->post_type ) {		$wrapper_block_type = 'core/navigation';	} elseif ( 'wp_block' === $post->post_type ) {		$wrapper_block_type = 'core/block';	} else {		$wrapper_block_type = 'core/post-content';	} 	$content = get_comment_delimited_block_content(		$wrapper_block_type,		$attributes,		$content	); 	/*	 * We need to avoid inserting any blocks hooked into the `before` and `after` positions	 * of the temporary wrapper block that we create to wrap the content.	 * See https://core.trac.wordpress.org/ticket/63287 for more details.	 */	$suppress_blocks_from_insertion_before_and_after_wrapper_block = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( $wrapper_block_type ) {		if (			$wrapper_block_type === $anchor_block_type &&			in_array( $relative_position, array( 'before', 'after' ), true )		) {			return array();		}		return $hooked_block_types;	};

Changelog

Introduced in 6.8.0. One change between 6.8.8 and 7.1.0.

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

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

7.0.4
Parameter $ignored_hooked_blocks_at_root added.verified against source
7.0.0
Added the $ignored_hooked_blocks_at_root parameter.from the docblock
6.8.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.