Returns a joined string of the aggregate serialization of the given parsed blocks.
Parameters
$blocks
array[]required- Array of block structures.
...$0
arrayA representative array of a single parsed block object. See WP_Block_Parser_Block.blockName
stringName of block.attrs
arrayAttributes from block comment delimiters.innerBlocks
array[]List of inner blocks. An array of arrays that have the same structure as this one.innerHTML
stringHTML from inside block comment delimiters.innerContent
arrayList of string fragments and null markers where inner blocks were found.
Source
function serialize_blocks( $blocks ) { return implode( '', array_map( 'serialize_block', $blocks ) ); }
Changelog
Version Description 5.3.1 Introduced. User Contributed Notes
You must log in before being able to contribute a note or feedback.
Used with
wp_update_post()
Sample of how to use
serialize_blocks()
after making adjustments to a post’s block content.In this use case, I wanted to create a cross-reference between the captions used on media in a gallery, and the captions saved to the media’s corresponding Attachment post. These can easily get out of date if a user is only managing them in one place or the other.
Gallery blocks have a caption field on the block editor view that is static to the block and does not save back to the original Attachment. This class updates the Attachment’s caption if it is empty while a static one was written in the a gallery block. It also does the converse; it adds a
tag with the Attachment’s caption value to the gallery items that don’t have a static caption written on them.
The functional use for this is to update the captions into the database so that the user will see them on page reload, instead of trusting that they will show up only in the front end (using the
render_block
filter).