apply_block_hooks_to_content( string $content, WP_Block_Template|WP_Post|array $context, callable $callback = 'insert_hooked_blocks' ): string
- Since
- 6.6.0, 6.7.0
- Source
wp-includes/blocks.php:1052
Compatibility
- WordPress
- since 6.7.0
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$contentstring- Serialized content.
$contextWP_Block_Template|WP_Post|array- A block template, template part,
wp_navigationpost object, or pattern that the blocks belong to. $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
- Scaling
- Scales with input
- Instructions
- 7–8
- Plugin surface
- None
- Called by
- 8
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 136.
Nothing here hands control to plugin code.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below apply_block_hooks_to_content() - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–8 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 136 | 7–8 | 18 | |
| 8.5 | 136 | 7–8 | 18 | |
| 8.4 | 136 | 7–8 | 18 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 139 | 7–8 | 18 | |
| 8.2 | 139 | 7–8 | 18 | |
| 8.1 | 139 | 7–8 | 18 | |
| 7.4 | 139 | 7–8 | 18 |
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 · 12
- 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()
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 8
- WP_Block_Patterns_Registry::get_all_registered()Retrieves all registered block patterns.
- WP_Block_Patterns_Registry::get_registered()Retrieves an array containing the properties of a registered block pattern.
- _build_block_template_result_from_file()Builds a unified template object based on a theme file.
- _build_block_template_result_from_post()Builds a unified template object based a post Object.
- block_core_navigation_insert_hooked_blocks()Insert hooked blocks into a Navigation block.
- inject_ignored_hooked_blocks_metadata_attributes()Inject ignoredHookedBlocks metadata attributes into a template or template part.
- insert_hooked_blocks_into_rest_response()Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
- update_ignored_hooked_blocks_postmeta()Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
Source code
function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) { $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 ), $before_block_visitor, $after_block_visitor ); remove_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );Changelog
Introduced in 6.6.0. One change between 6.7.10 and 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
$context retyped from WP_Block_Template|WP_Post|array to WP_Block_Template|WP_Post|array|null.verified against sourcetheme attribute into Template Part blocks, even if no hooked blocks are registered.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.10 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.