Search code examples
javascriptreactjswordpresswordpress-gutenberggutenberg-blocks

How to "manually" (programmatically) insert a block in Gutenberg?


Gutenberg's API is quiet obscure and I can't figure how to create and append a block to a post.

I've found the wp.blocks.createBlock('core/paragraph', {content: "blabla"}); which returns a pretty block object, but does not append any content to the post.

I would like to insert a simple paragraph with some custom content just by clicking a button.


Solution

  • var content = "Test content";
    var el = wp.element.createElement;
    var name = 'core/paragraph';
    // var name = 'core/html';
    insertedBlock = wp.blocks.createBlock(name, {
        content: content,
    });
    wp.data.dispatch('core/editor').insertBlocks(insertedBlock);