Programmatically trigger the gutenberg save

I have a load of data as JSON that I am trying to save into a fresh install of WordPress.

I am using puppeteer to log into and create each page, and wp.blocks.createBlock to programmatically create the blocks.

// Input the page title

    const headingInput = document.querySelector("#post-title-0");
    headingInput.innerHTML = json.data.heading;

    var changeEvent = new Event("change", {
        bubbles: true,
        cancelable: true,
    });
    headingInput.dispatchEvent(changeEvent);


    /**
     * Next up, create each of the gutenberg blocks
     */
    json.data.blocks.forEach((block) => {
        if (
            block.className == "feature text-feature"
        ) {
            block.content.forEach((element) => {
                if (element.hasOwnProperty("gutenbergBlock")) {
                    var el = wp.element.createElement;
                    insertedBlock = wp.blocks.createBlock(
                        element.gutenbergBlock,
                        element.attributes
                    );
                    wp.data.dispatch("core/editor").insertBlocks(insertedBlock);
                }
            });
        }
    });


 // Attempt to trigger the save:
    document
        .querySelectorAll(".editor-post-publish-button")
        .forEach((button) => {
            console.log("clicking button!");
            var clickEvent = new MouseEvent("click", {
                view: window,
                bubbles: true,
                cancelable: false,
            });
            button.dispatchEvent(clickEvent);
        });

Above, I attempt to click on the save button to save the post, however this part does not work.

Is there a way to trigger the saving of page content? Something like: wp.blocks.save()? I have trawled the documentation but can’t find anything so niche.

1 Answer
1

Have you tried using savePost?

The Post Editor’s Data

Leave a Comment