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
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
- Scaling
- Scales with input
- Instructions
- 7–8
- Plugin surface
- None
- Called by
- 9
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 141.
Nothing here hands control to plugin code.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - 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 | 141 | 7–8 | 19 | |
| 8.5 | 141 | 7–8 | 19 | |
| 8.4 | 141 | 7–8 | 19 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 144 | 7–8 | 19 | |
| 8.2 | 144 | 7–8 | 19 | |
| 8.1 | 144 | 7–8 | 19 | |
| 7.4 | 144 | 7–8 | 19 |
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
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 9
- 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.
- apply_block_hooks_to_content_from_post_object()Run the Block Hooks algorithm on a post object's content.
- get_block_file_template()Retrieves a unified template object based on a theme file or plugin registration.
- get_block_templates()Retrieves a list of unified template objects based on a query.
- inject_ignored_hooked_blocks_metadata_attributes()Inject ignoredHookedBlocks metadata attributes into a template or template part.
- 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 = 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.
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 source$context parameter default to null, in which case get_post() will be called to use the current post as context.from the docblocktheme attribute into Template Part blocks, even if no hooked blocks are registered.from the docblockAbout 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.