WP_Block::render( array $options = array() ): string
- Since
- 5.5.0, 6.5.0
- Source
wp-includes/class-wp-block.php:492
Compatibility
- WordPress
- since 6.5.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
$optionsarrayoptional- Optional options object.Default:
array()$dynamicboolDefaults to 'true'. Optionally set to false to avoid using the block's render_callback.
Return value
string- Rendered block output.
Performance profile
How much work a call to WP_Block::render() 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
- Light
- Scaling
- Scales with input
- Instructions
- 107–166
- Plugin surface
- 7 hooks
- Called by
- 0
Touches nothing outside its own arguments.
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 338.
Third-party callbacks on 'interactivity_process_directives', 'pre_render_block', 'render_block_data' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block::render() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block !== false | 107–132 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action() |
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block === false | 112–137 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_interactivity_process_directives(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action() |
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block !== false && $before_wp_enqueue_scripts_count === false && apply_filters() | 119–140 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action(), apply_filters() |
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block === false && $before_wp_enqueue_scripts_count === false && apply_filters() | 124–145 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_interactivity_process_directives(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action(), apply_filters() |
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block !== false && $before_wp_enqueue_scripts_count === false && !apply_filters() | 137–161 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action(), apply_filters(), array_diff(), array_diff(), array_diff() |
empty($computed_attributes) && !$options && empty($value) && $root_interactive_block === false && $before_wp_enqueue_scripts_count === false && !apply_filters() | 142–166 | did_action(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), apply_filters(), wp_parse_args(), ->process_block_bindings(), apply_filters(), apply_filters(), wp_interactivity_process_directives(), wp_styles(), wp_scripts(), wp_script_modules(), ->get_queue(), did_action(), apply_filters(), array_diff(), array_diff(), array_diff() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 338 | 107–166 | 50 | |
| 8.5 | 338 | 107–166 | 50 | |
| 8.4 | 338 | 107–166 | 50 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 340 | 107–168 | 50 | |
| 8.2 | 340 | 107–168 | 50 | |
| 8.1 | 340 | 107–168 | 50 | |
| 7.4 | 340 | 107–168 | 50 |
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.
Hooks and filters fired · 7
7 hooks fire while WP_Block::render() runs, in this order:
- apply_filters( interactivity_process_directives )filterline 514 (+22 into the body)
Filters whether Interactivity API should process directives.
- apply_filters( pre_render_block )filterline 552 (+60 into the body)
Allows render_block() to be short-circuited, by returning a non-null value.
- apply_filters( render_block_data )filterline 561 (+69 into the body)
Filters the block being rendered in render_block(), before it's processed.
- apply_filters( render_block_context )filterline 564 (+72 into the body)
Filters the default context provided to a rendered block.
- apply_filters( render_block )filterline 648 (+156 into the body)
Filters the content of a single block.
- apply_filters( render_block_{$this->name} )filterline 663 (+171 into the body)
Filters the content of a single block.
- apply_filters( enqueue_empty_block_content_assets )filterline 701 (+209 into the body)
Filters whether to enqueue assets for a block which has no rendered content.
Uses · 16
- did_action()Retrieves the number of times an action has been fired during the current request.
- wp_styles()Initializes $wp_styles if it has not been set.
- wp_scripts()Initializes $wp_scripts if it has not been set.
- wp_script_modules()Retrieves the main WP_Script_Modules instance.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_enqueue_script()Enqueues a script.
- wp_enqueue_script_module()Marks the script module to be enqueued in the page.
- wp_enqueue_style()Enqueues a CSS stylesheet.
- wp_interactivity_process_directives()Processes the interactivity directives contained within the HTML content and updates the markup accordingly.
- wp_dequeue_style()Removes a previously enqueued CSS stylesheet.
- wp_dequeue_script()Removes a previously enqueued script.
Show all 16
- wp_dequeue_script_module()Unmarks the script module so it is no longer enqueued in the page.
- wp_script_modules()::get_queue()
- WP_Block::process_block_bindings()Processes the block bindings and updates the block attributes with the values from the sources.
- WP_Block::replace_html()Depending on the block attribute name, replace its value in the HTML based on the value provided.
Source code
public function render( $options = array() ) { global $post; $before_wp_enqueue_scripts_count = did_action( 'wp_enqueue_scripts' ); // Capture the current assets queues. $before_styles_queue = wp_styles()->queue; $before_scripts_queue = wp_scripts()->queue; $before_script_modules_queue = wp_script_modules()->get_queue(); /* * There can be only one root interactive block at a time because the rendered HTML of that block contains * the rendered HTML of all its inner blocks, including any interactive block. */ static $root_interactive_block = null; /** * Filters whether Interactivity API should process directives. * * @since 6.6.0 * * @param bool $enabled Whether the directives processing is enabled. */ $interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true ); if ( $interactivity_process_directives_enabled && null === $root_interactive_block && ( ( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) || ! empty( $this->block_type->supports['interactivity']['interactive'] ) ) ) { $root_interactive_block = $this; } $options = wp_parse_args( $options, array( 'dynamic' => true, ) ); // Process the block bindings and get attributes updated with the values from the sources. $computed_attributes = $this->process_block_bindings(); if ( ! empty( $computed_attributes ) ) { // Merge the computed attributes with the original attributes. $this->attributes = array_merge( $this->attributes, $computed_attributes ); } $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); $block_content = ''; if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { $index = 0; foreach ( $this->inner_content as $chunk ) { if ( is_string( $chunk ) ) { $block_content .= $chunk; } else { $inner_block = $this->inner_blocks[ $index ]; $parent_block = $this; /** This filter is documented in wp-includes/blocks.php */ $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); if ( ! is_null( $pre_render ) ) { $block_content .= $pre_render; } else { $source_block = $inner_block->parsed_block; $inner_block_context = $inner_block->context; /** This filter is documented in wp-includes/blocks.php */ $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); /** This filter is documented in wp-includes/blocks.php */ $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); /* * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`. * Therefore the second condition is irrelevant if the first one is satisfied. */ if ( $inner_block->context !== $inner_block_context ) { $inner_block->refresh_context_dependents();Changelog
Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wp-block.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.