Skip to content

Instantly share code, notes, and snippets.

@bfintal
Last active April 11, 2024 00:22
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bfintal/beebe46593df25326c0271f4cb227d96 to your computer and use it in GitHub Desktop.
Save bfintal/beebe46593df25326c0271f4cb227d96 to your computer and use it in GitHub Desktop.
Gutenberg Cheat Sheet
// Get the data of a block
wp.data.select( 'core/block-editor' ).getBlocks()[0]
// Update attributes of another block
// wp.data.dispatch( 'core/editor' ).updateBlockAttributes( clientID, attributes )
wp.data.dispatch( 'core/block-editor' ).updateBlockAttributes( '10d88a6d-95d6-4e07-8293-5f59c83a26c0', { heading: 'New Heading' } )
// Get currently selected block.
wp.data.select( 'core/block-editor' ).getBlockSelectionStart()
// Count the total number of blocks in the editor (includes even nested)
var a = innerBlocks => {
return innerBlocks.length + innerBlocks.reduce( ( sum, block ) => {
return sum + a(block.innerBlocks)
}, 0 )
}
a( wp.data.select( 'core/block-editor' ).__unstableGetClientIdsTree() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment