Search code examples
javascriptwordpresswordpress-gutenberggutenberg-blocks

Add content programmatically in WordPress Gutenberg Editor


I am trying to add content to the WordPress Gutenberg editor using JavaScript. After saving and reloading the page, I receive this error.

I retrieve the content from another API using WP_Remote_Request. It retrieves the data properly and responds correctly. Then, I append it correctly and save it. However, when I reload the page, I receive this error.

This block contains unexpected or invalid content.

The error screenshot

I am trying this.

let content = `<h1>What is Lorem Ipsum?</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>`

let newBlock = wp.blocks.createBlock( 'core/paragraph', { content: content } );
            let serializedBlock = wp.blocks.serialize( newBlock );
            wp.data.dispatch( 'core/block-editor' ).insertBlock( newBlock, wp.data.select( 'core/block-editor' ).getBlockCount() );
            let parsedBlock = wp.blocks.parse( serializedBlock );
            newBlock = Object.assign( {}, newBlock, parsedBlock );
            wp.data.dispatch( 'core/block-editor' ).replaceBlock( newBlock.clientId, newBlock );
            wp.data.dispatch( 'core/editor' ).savePost();

The response after reloading is This block contains unexpected or invalid content.


Solution

  • The issue is the paragraph block expects only paragraph content. Your variable let content=... also contains a <h1>..</h1> heading that needs to be added separately with a heading block in order for the paragraph block to validate.

    The other step you have added of serializing the block is not needed if the blocks are added to the Editor correctly; savePost() will serialize the content as expected if the block are valid, eg:

    JavaScript

    // Define content of Heading block - content between '<h{1-6}>'
    let heading = `What is Lorem Ipsum?`;
    let newHeadingBlock = wp.blocks.createBlock( 'core/heading', { content: heading } );
    
    // Define content of Paragraph block - content between '<p>...</p>'
    let paragraph = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
    let newParagraphBlock = wp.blocks.createBlock( 'core/paragraph', { content: paragraph } );
    
    // Add the new blocks to the Editor with insertBlocks for multiple block in a given order
    wp.data.dispatch( 'core/block-editor' ).insertBlocks([newHeadingBlock,newParagraphBlock]);
    
    // Save the post content
    wp.data.dispatch( 'core/editor' ).savePost();
    

    If you look at the paragraph block.json, you will see that it selects its content from the HTML via the p selector and the heading block.json uses the h{level} selector. This will help you avoid validation errors by providing what the block expects.