Help getting Gutenberg blocks to XML


I’m trying to get Gutenberg blocks as XML so I can export the content but I’m not sure how to do it properly. This is what I have:

function get_blocks($content) {
    $blocks = parse_blocks($content);
    foreach ($blocks as $block) {

        if ($block['blockName'] == 'core/gallery') {
            $xml .= "**LT**Gallery**GT**";
            $xml .= $block['innerHTML'];
            $xml .= "**LT**/Gallery**GT**";
        } elseif ($block['blockName'] == 'core/paragraph') {
            $xml .= "**LT**Paragraph**GT**";
            $xml .= $block['innerHTML'];
            $xml .= "**LT**/Paragraph**GT**";
        } elseif ($block['blockName'] == 'core/video') {
            $xml .= "**LT**Video**GT**";
            $xml .= $block['innerHTML'];
            $xml .= "**LT**/Video**GT**";
        } else {

            $xml .= "Nothing";

    return $xml;


I can get the parsed content when returning $blocks (as an array) but the foreach block always returns “Nothing”.

Can someone please point me in the right direction? What am I doing wrong? This is for a custom WP All Export feed.

Thank you.

Frederico Leonardo 2 years 2021-12-26T09:06:59-05:00 0 Answers 0 views 0

Leave an answer