serialize_block example


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
    );
}
if (
            'core/template-part' === $block['blockName'] &&
            ! isset( $block['attrs']['theme'] )
        ) {
            $block['attrs']['theme'] = get_stylesheet();
            $has_updated_content     = true;
        }
    }

    if ( $has_updated_content ) {
        foreach ( $template_blocks as &$block ) {
            $new_content .= serialize_block( $block );
        }

        return $new_content;
    }

    return $template_content;
}

/** * Parses a block template and removes the theme attribute from each template part. * * @since 5.9.0 * @access private * * @param string $template_content Serialized block template content. * @return string Updated block template content. */
Home | Imprint | This part of the site doesn't use cookies.