Help getting Gutenberg blocks to XML

Question

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);
    $xml="";
    
    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.

0
Frederico Leonardo 8 months 2021-12-26T09:06:59-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse