serialize_block( array $block ): string
- Since
- 5.3.1
- Source
wp-includes/blocks.php:1795
Description
This should be used when preparing a block to be saved to post content.
Prefer render_block when preparing a block for display. Unlike render_block, this does not evaluate a block's render_callback, and will instead preserve the markup as parsed.
Compatibility
- WordPress
- since 5.3.1
- 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
$blockarray- An associative array of a single parsed block object. See WP_Block_Parser_Block.
$blockNamestring|nullName of block.$attrsarrayAttributes from block comment delimiters.$innerBlocksarray[]List of inner blocks. An array of arrays that have the same structure as this one.$innerHTMLstringHTML from inside block comment delimiters.$innerContentarrayList 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 serialize_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
- Light
- Scaling
- Scales with input
- Instructions
- 19–22
- Plugin surface
- None
- Called by
- 5
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 36.
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 one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost serialize_block() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 19–22 | get_comment_delimited_block_content() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 35 | 19–22 | 4 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 36 | 19–22 | 4 | |
| 8.4 | 36 | 19–22 | 4 | |
| 8.3 | 36 | 19–22 | 4 | |
| 8.2 | 36 | 19–22 | 4 | |
| 8.1 | 36 | 19–22 | 4 | |
| 7.4 | 36 | 19–22 | 4 |
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 · 2
- serialize_block()Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.
- get_comment_delimited_block_content()Returns the content of a block, including comment delimiters.
Used by · 5
- _inject_theme_attribute_in_block_template_content()Parses wp_template content and injects the active theme's stylesheet as a theme attribute into each wp_template_part
- _remove_theme_attribute_in_block_template_content()Parses a block template and removes the theme attribute from each template part.
- filter_block_content()Filters and sanitizes block content to remove non-allowable HTML from parsed block attribute values.
- insert_hooked_blocks()Returns the markup for blocks hooked to the given anchor block in a specific relative position.
- serialize_block()Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.
Source code
function serialize_block( $block ) { $block_content = ''; $index = 0; foreach ( $block['innerContent'] as $chunk ) { $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] ); } if ( ! is_array( $block['attrs'] ) ) { $block['attrs'] = array(); } return get_comment_delimited_block_content( $block['blockName'], $block['attrs'], $block_content );}Changelog
Introduced in 5.3.1. 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.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.