serialize_block_attributes() WordPress Function

The serialize_block_attributes() function is used to convert block attributes into a JSON string. This is useful for storing and retrieving data in a format that can be easily read and written by both the WordPress block editor and external applications.

serialize_block_attributes( array $block_attributes ) #

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


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.


Top ↑

Parameters

$block_attributes

(array)(Required)Attributes object.


Top ↑

Return

(string) Serialized attributes.


Top ↑

Source

File: wp-includes/blocks.php

function serialize_block_attributes( $block_attributes ) {
	$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
	$encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes );
	$encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes );
	$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes );
	$encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes );
	// Regex: /\\"/
	$encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes );

	return $encoded_attributes;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.1Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.