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
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
- Scaling
- Constant
- Instructions
- 7–9
- Plugin surface
- None
- Called by
- 5
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 123.
Nothing here hands control to plugin code.
5 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_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–9 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 123 | 7–9 | 11 | |
| 8.5 | 123 | 7–9 | 11 | |
| 8.4 | 123 | 7–9 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 126 | 7–9 | 11 | |
| 8.2 | 126 | 7–9 | 11 | |
| 8.1 | 126 | 7–9 | 11 | |
| 7.4 | 126 | 7–9 | 11 |
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
- get_post()Retrieves post data given a post ID or post object.
- apply_block_hooks_to_content()Runs the hooked blocks algorithm on the given content.
- has_blocks()Determines whether a post or content string has blocks.
- get_comment_delimited_block_content()Returns the content of a block, including comment delimiters.
- get_post_meta()Retrieves a post meta field for the given post ID.
- add_filter()Adds a callback function to a filter hook.
- remove_filter()Removes a callback function from a filter hook.
- extract_serialized_parent_block()Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the wrapper block.
- parse_blocks()Parses blocks out of a content string.
- remove_serialized_parent_block()Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks.
Used by · 5
- WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post()Gets the inner blocks for the navigation block from the navigation post.
- WP_Navigation_Block_Renderer::get_overlay_blocks_from_template_part()Gets the inner blocks for the navigation block from an overlay template part.
- block_core_navigation_get_fallback_blocks()Retrieves the appropriate fallback to be used on the front of the site when there is no menu assigned to the Nav block.
- insert_hooked_blocks_into_rest_response()Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
- render_block_core_block()Renders the `core/block` block on server.
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.
Signature, return type and hooks compared across 4 parsed releases.
$ignored_hooked_blocks_at_root added.verified against source$ignored_hooked_blocks_at_root parameter.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.