Firing Wordpress Gutenberg "Convert to Blocks" programmatically Firing Wordpress Gutenberg "Convert to Blocks" programmatically wordpress wordpress

Firing Wordpress Gutenberg "Convert to Blocks" programmatically


May be this is a little late, but if someone is still looking for a solution here's how you do it.

This code assumes that your classic block is the first one:

var block = wp.data      .select("core/editor")      .getBlocks()[0];wp.data.dispatch( 'core/editor' ).replaceBlocks(block.clientId, wp.blocks.rawHandler(   { HTML: wp.blocks.getBlockContent( block ) }));

If you want to do it for all classic blocks, simply iterate overall blocks and look for block name core/freeform to convert it like this:

wp.data.select("core/editor").getBlocks().forEach(function(block, blockIndex){  if (block.name === "core/freeform"){    wp.data.dispatch( 'core/editor' ).replaceBlocks(block.clientId, wp.blocks.rawHandler(       { HTML: wp.blocks.getBlockContent( block ) }    ));      }})