You could start by adding:
// What to do here?
wp.data.dispatch('core/block-editor').insertDefaultBlock({content:'xxxxxx'});
This will insert a block with the specified text to the end of the blocks list when the user will hit the update button. Once, and then it will self-unsubscribe.
To run it every time the user hits the update button, you don’t want to unsubscribe()
. This example will add xxxxxx
every time:
const editor = wp.data.select("core/editor");
let wasSavingPost = editor.isSavingPost();
let wasAutosavingPost = editor.isAutosavingPost();
const unsubscribe = wp.data.subscribe(() => {
const isSavingPost = editor.isSavingPost();
const isAutosavingPost = editor.isAutosavingPost();
const shouldTriggerAddXXXXXX =
wasSavingPost &&
!wasAutosavingPost &&
!isSavingPost &&
"page" === editor.getCurrentPost().type;
wasSavingPost = isSavingPost;
wasAutosavingPost = isAutosavingPost;
if (shouldTriggerAddXXXXXX) {
wp.data
.dispatch("core/block-editor")
.insertDefaultBlock({ content: "xxxxxx" });
}
});
Note that it will not update the post in the database after last insertion of xxxxxx
, because code is executed after saving.
You can learn more about this sort of thing in the Reference Guides of the Block Editor Handbook.