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

---

# serialize_block_attributes( array $block_attributes ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#changelog)

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

Given an array of attributes, returns a string in the serialized attributes format
prepared for post content.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/serialize_block_attributes/?output_format=md#description)󠁿

The serialized result is a JSON-encoded string, with unicode escape sequence substitution
for characters which might otherwise interfere with embedding the result in an HTML
comment.

This function must produce output that remains in sync with the output of the serializeAttributes
JavaScript function in the block editor in order to ensure consistent operation 
between PHP and JavaScript.

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

 `$block_attributes`arrayrequired

Attributes object.

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

 string Serialized attributes.

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

    ```php
    function serialize_block_attributes( $block_attributes ) {
    	$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

    	return strtr(
    		$encoded_attributes,
    		array(
    			'\\\\' => '\\u005c',
    			'--'   => '\\u002d\\u002d',
    			'<'    => '\\u003c',
    			'>'    => '\\u003e',
    			'&'    => '\\u0026',
    			'\\"'  => '\\u0022',
    		)
    	);
    }
    ```

[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#L1623)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/blocks.php#L1623-L1637)

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

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

Encodes a variable into JSON, with some confidence checks.

  |

| Used by | Description | 
| [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.

  |

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

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

## User Contributed Notes

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