2

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
  • I don't know Puppeteer that well, but if you can trigger keyboard shortcuts then Command/Control + S triggers a save. Commented Sep 9, 2021 at 18:30

2 Answers 2

2

You can do this with:

wp.data.dispatch( 'core/editor' ).savePost()
0

Have you tried using savePost?

https://developer.wordpress.org/block-editor/reference-guides/data/data-core-editor/#savepost

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.