Title: serialize_block
Published: April 1, 2020
Last modified: February 24, 2026

---

# serialize_block( array $block ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#wp--skip-link--target)

Returns the content of a block, including comment delimiters, serializing all attributes
from the given parsed block.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#parameters)󠁿

 `$block`arrayrequired

An associative array of a single parsed block object. See [WP_Block_Parser_Block](https://developer.wordpress.org/reference/classes/wp_block_parser_block/).

 * `blockName` string|null
 * Name of block.
 * `attrs` array
 * Attributes from block comment delimiters.
 * `innerBlocks` array[]
 * List of inner blocks. An array of arrays that have the same structure as this
   one.
 * `innerHTML` string
 * HTML from inside block comment delimiters.
 * `innerContent` array
 * List of string fragments and null markers where inner blocks were found.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#return)󠁿

 string String of rendered HTML.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#source)󠁿

    ```php
    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
    	);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/blocks.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/blocks.php#L1713)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/blocks.php#L1713-L1730)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#related)󠁿

| Uses | Description | 
| [serialize_block()](https://developer.wordpress.org/reference/functions/serialize_block/)`wp-includes/blocks.php` |

Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.

  | 
| [get_comment_delimited_block_content()](https://developer.wordpress.org/reference/functions/get_comment_delimited_block_content/)`wp-includes/blocks.php` |

Returns the content of a block, including comment delimiters.

  |

| Used by | Description | 
| [insert_hooked_blocks()](https://developer.wordpress.org/reference/functions/insert_hooked_blocks/)`wp-includes/blocks.php` |

Returns the markup for blocks hooked to the given anchor block in a specific relative position.

  | 
| [_inject_theme_attribute_in_block_template_content()](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/)`wp-includes/deprecated.php` |

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()](https://developer.wordpress.org/reference/functions/_remove_theme_attribute_in_block_template_content/)`wp-includes/deprecated.php` |

Parses a block template and removes the theme attribute from each template part.

  | 
| [filter_block_content()](https://developer.wordpress.org/reference/functions/filter_block_content/)`wp-includes/blocks.php` |

Filters and sanitizes block content to remove non-allowable HTML from parsed block attribute values.

  | 
| [serialize_block()](https://developer.wordpress.org/reference/functions/serialize_block/)`wp-includes/blocks.php` |

Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.3.1](https://developer.wordpress.org/reference/since/5.3.1/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/serialize_block/?output_format=md#comment-content-6013)
 2.   [Muhammad Saqib Sarwar](https://profiles.wordpress.org/saqibsarwar/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/serialize_block/#comment-6013)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fserialize_block%2F%23comment-6013)
    Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fserialize_block%2F%23comment-6013)
 4. A sample code to understand how this works. I am using [https://developer.wordpress.org/cli/commands/eval-file/](https://developer.wordpress.org/cli/commands/eval-file/)
    to execute this.
 5.     ```php
        /*
         * Create a block programmatically and serialize it.
         */
        $block_name = 'core/paragraph';
        $innerHTML  = 'Sample paragraph text.';
    
        $converted_block = new WP_Block_Parser_Block( $block_name, array(), array(), $innerHTML, array( $innerHTML ) );
        WP_CLI::log( print_r( $converted_block, true ) );
    
        $serialized_block = serialize_block( (array) $converted_block );
        WP_CLI::log( $serialized_block );
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fserialize_block%2F%3Freplytocom%3D6013%23feedback-editor-6013)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fserialize_block%2F)
before being able to contribute a note or feedback.