There’s probably a simpler way to do this, and if there isn’t you should open an issue on the Gutenberg GH issue tracker ( the API is not set in stone ).
Because the API is not set in stone, this answer may be useless when it’s finally done and merged. The first and best place to ask GB questions is on GitHub
You can eliminate all blocks using this:
wp.data.dispatch( 'core/editor' ).resetBlocks([]);
You can then create a new block programmatically:
let block = wp.blocks.createBlock( 'core/paragraph' );
Add text to it:
block.attributes.content.push( 'hello world' );
And insert it like this:
wp.data.dispatch( 'core/editor' ).insertBlocks( block );
On further inspection, there’s a simpler method:
let block = wp.blocks.createBlock( 'core/paragraph', { content: 'test' } );
wp.data.dispatch( 'core/editor' ).insertBlocks( block );
Related Posts:
- What are all the query parameters for getEntityRecords?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- How to only enqueue block javascript on the frontend when its needed [duplicate]
- Extend core block in Gutenberg
- Gutenberg custom block plugin with custom image sizes
- Get loading state of wp data selector
- Is there a core Sortable component in Gutenberg?
- Hooking a callback into the code within a Gutenberg block
- initial open/toggle PluginDocumentSettingPanel panel in document setting block editor gutenberg
- Is there an equivalent of the PHP function sanitize_key in Gutenberg?
- Correctly handling WordPress core data retrieval in Gutenberg
- Is there a client side API for handling transients or options?
- Gutenberg Modify core taxonomy panel element via wp.hooks.addFilter
- What is “open()” in MediaUpload?
- Adding a text element in between title and blocks container
- Setting HTML properties in a Gutenberg plugin using WordPress settings
- Gutenberg RichText
- How to handle Gutenberg wp.data async errors?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- How to get value of selected page template in Gutenberg editor?
- How do I access site and block editor state data and use `useSelect()` or `withSelect()` to bind it to my components?
- @wordpress/components Button variants not styled
- Block Editor: How to get title and post content within the WordPress admin UI in Javascript?
- Custom block SelectControl works but equivalent ComboboxControl gives errors – where’s my mistake?
- How can I add Block Style support to the core HTML block in Gutenberg?
- Gutenberg add extra attributes to custom format
- event/callback on block update?
- can a buttongroup have a label?
- How to disable inline css styles generated by Gutenberg editor?
- Gutenberg getMedia() in post query doesn’t return all featured images
- InnerBlocks restricting does not work on nested blocks
- How to transform a legacy widget into a block
- Gutenberg: Block validation Failed Richtext undefined
- Add A Tag To A Post Through JavaScript In Gutenberg UI?
- Gutenberg select categories
- Add custom HTML markup to Gutenberg RichText
- How to return div with custom attributes(Coding a Gutenberg Block)
- What is the correct way to import the blocks-editor?
- Open MediaUpload from external component in Gutenberg
- Get Block Attributes in JSON REST API
- Open Featured Image Modal in WordPress Gutenberg onClick of a button
- Gutenberg extend blocks add new class name
- Search for a keyword across post types in a Gutenberg component
- Using apiFetch for retrieving post data in Gutenberg
- Add custom classes for blocks in editor based on custom attributes
- Looping over wordpress meta to create “ ‘s?
- How to display post content in the block editor
- How to load an additional script for a block in the block editor?
- useSelect second parameter
- Gutenberg consume wp-json data and reflect in frontend the content
- Trigger wp-embed via JavaScript to refresh iframe preview?
- Gutenberg getBlockIndex in save() function
- Widgets and Post/Page edit/new blank, console errors only, no server errors
- Trying to turn the Edit function into a class to be able to use React lifecycle methods
- Gutenberg richtext block vaildation failed
- Conditional save return on Gutenberg Block
- Can I alter the block editor’s paste text behavior?
- Custom Gutenberg-Block esnext pass variables
- How to read inline-CSS from Gutenberg block?
- Gutenberg RangeControl
- Custom Gutenberg block: access dom element via JavaScript
- Custom Block – save.js function not saving attributes
- Add a Page Screen is Visually Blank
- Including dependencies using @wordpress/dependency-extraction-webpack-plugin
- Gutenberg sidebar show input field on toggle
- Get the ID of a page in Parent combobox in editor
- Uncaught TypeError: r is not a function
- Custom block update rendering when reused
- WordPress Gutenberg Ajax request
- WP Gutenberg – custom block with two content fields
- How do I make a savable preview like Youtube Gutenberg block?
- iFrame onLoad in custom Gutenberg block
- Block pattern conflict with custom block
- WordPress Gutenberg: Attribute overwritten by block duplicate
- Table block variation with header section activated
- Filtering Gutenberg Components, not Blocks
- Where to include JS library in gutenberg blocks
- How to detect if we are in the Site Editor part of the Block Editor (as opposed to editing a Page/Post) in JavaScript?
- How can I show a Slot/Fill in the block editor
- Is it possible to add javascript to template parts
- How to add a new attribute to core wp block editor without npm?
- How to add more elements to an already existing section in Gutenberg
- Working with a non-React external library in a custom block
- Create Youtube embed block with createBlock
- Masonry gallery block is working in the block editor but not the template editor
- How to change the selected Template using javascript?
- How to import the imagesLoaded and Masonry libs that come with WP in a Gutenberg block?
- config @wordpress/scripts with webpack and postCSS features
- Jest Unit tests for High order component containing Block Editor
- How to build BOTH non-block components and blocks present in the /src directory using @wordpress/scripts
- How to transform a shortcode into a block
- Metabox conditionals depending on post format and template in Gutenberg
- How to re-render Gutenberg component when object instance is available
- How to access current discussion settings in gutenberg via JS?
- Gutenberg DatePicker component with time set to zero
- How to render HTML content using the Interactivity API?
- How to auto rename JS files to prevent browser cache issues
- How to get selected category objects in the block editor?
- How to render initial posts on page load in a Gutenberg block using the Interactivity API?
- How to include block style variations for blocks in posts dynamically rendered via the Interactivity API