wppaste
WordPress

render_block( array $parsed_block ): string

Since
5.0.0
Source
wp-includes/blocks.php:2089
Renders a single block into a HTML string.

Compatibility

WordPress
since 5.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 every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$parsed_blockarray
An associative array of the block being rendered. See WP_Block_Parser_Block.
  • $blockNamestring

    Name of block.
  • $attrsarray

    Attributes from block comment delimiters.
  • $innerBlocksarray[]

    List of inner blocks. An array of arrays that have the same structure as this one.
  • $innerHTMLstring

    HTML from inside block comment delimiters.
  • $innerContentarray

    List of string fragments and null markers where inner blocks were found.

Return value

string
String of rendered HTML.

Performance profile

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
13–44

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

Plugin surface
3 hooks

Third-party callbacks on 'pre_render_block', 'render_block_data', 'render_block_context' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$pre_render !== null13apply_filters()
$pre_render === null38–44apply_filters(), apply_filters(), apply_filters(), ->render()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 45 instructions, 13–44 executed per call, 2 branches. The work does not change between versions.

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

3 hooks fire while render_block() runs, in this order:

  1. apply_filters( pre_render_block )filterline 2113 (+24 into the body)

    Allows render_block() to be short-circuited, by returning a non-null value.

  2. apply_filters( render_block_data )filterline 2151 (+62 into the body)

    Filters the block being rendered in render_block(), before it's processed.

  3. apply_filters( render_block_context )filterline 2187 (+98 into the body)

    Filters the default context provided to a rendered block.

Uses · 2

Used by · 5

Source code

function render_block( $parsed_block ) {	global $post;	$parent_block = null; 	/**	 * Allows render_block() to be short-circuited, by returning a non-null value.	 *	 * @since 5.1.0	 * @since 5.9.0 The `$parent_block` parameter was added.	 *	 * @param string|null   $pre_render   The pre-rendered content. Default null.	 * @param array         $parsed_block {	 *     An associative array of the block being rendered. See WP_Block_Parser_Block.	 *	 *     @type string   $blockName    Name of block.	 *     @type array    $attrs        Attributes from block comment delimiters.	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that	 *                                  have the same structure as this one.	 *     @type string   $innerHTML    HTML from inside block comment delimiters.	 *     @type array    $innerContent List of string fragments and null markers where	 *                                  inner blocks were found.	 * }	 * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.	 */	$pre_render = apply_filters( 'pre_render_block', null, $parsed_block, $parent_block );	if ( ! is_null( $pre_render ) ) {		return $pre_render;	} 	$source_block = $parsed_block; 	/**	 * Filters the block being rendered in render_block(), before it's processed.	 *	 * @since 5.1.0	 * @since 5.9.0 The `$parent_block` parameter was added.	 *	 * @param array         $parsed_block {	 *     An associative array of the block being rendered. See WP_Block_Parser_Block.	 *	 *     @type string   $blockName    Name of block.	 *     @type array    $attrs        Attributes from block comment delimiters.	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that	 *                                  have the same structure as this one.	 *     @type string   $innerHTML    HTML from inside block comment delimiters.	 *     @type array    $innerContent List of string fragments and null markers where	 *                                  inner blocks were found.	 * }	 * @param array         $source_block {	 *     An un-modified copy of `$parsed_block`, as it appeared in the source content.	 *     See WP_Block_Parser_Block.	 *	 *     @type string   $blockName    Name of block.	 *     @type array    $attrs        Attributes from block comment delimiters.	 *     @type array[]  $innerBlocks  List of inner blocks. An array of arrays that	 *                                  have the same structure as this one.	 *     @type string   $innerHTML    HTML from inside block comment delimiters.	 *     @type array    $innerContent List of string fragments and null markers where	 *                                  inner blocks were found.	 * }	 * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.	 */	$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block, $parent_block ); 	$context = array(); 	if ( $post instanceof WP_Post ) {		$context['postId'] = $post->ID; 		/*		 * The `postType` context is largely unnecessary server-side, since the ID		 * is usually sufficient on its own. That being said, since a block's		 * manifest is expected to be shared between the server and the client,		 * it should be included to consistently fulfill the expectation.		 */		$context['postType'] = $post->post_type;	} 	/**	 * Filters the default context provided to a rendered block.

Changelog

Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.

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

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

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.