Add PHP block template to content using wp_insert_post

I’ve modified serialize_block a bit:

function serialize_block_template( $block ) {
    $block_content="";

    if (isset($block[2])) {
        $index = 0;
        foreach ( $block[2] as $chunk ) {
            $block_content .= is_string( $chunk ) ? $chunk : serialize_block_template( $block[2][ $index++ ] );
        }
    }

    return get_comment_delimited_block_content(
        $block[0],
        $block[1],
        $block_content
    );
}

Disclaimer: could have some bugs, only tested it for one simple use case.

Leave a Comment