How can I add arbitrary `data-` attributes to a block’s `edit()` container?

You can define a getEditWrapperProps function when you register the block, and return an object that sets the props for the wrapper. The function will be passed the props for the block, in case those are needed. registerBlockType( ‘wpse/example-get-edit-wrapper-props’, { title : ‘getEditWrapperProps() Example’, // … getEditWrapperProps( props ) { return { ‘data-foo’: ‘bar’, ‘data-quix’: … Read more

Gutenberg Custom Block Getting All Posts

In addition to loading the wp-api script as mentioned here: wp_enqueue_script( ‘wp-api’ ); // or use below when enqueueing as dependency //wp_enqueue_script( ‘my_script’, ‘path/to/my/script’, array( ‘wp-api’ ) ); You should also know that each collection is a function (equivalent to a class in PHP), so you need to use the new keyword to instantiate a … Read more

get used blocks in post and detect changing

You can access the blocks used in the post in js using getBlocks(): wp.data.select( ‘core/block-editor’ ).getBlocks(); In terms of updating on changes – the editor saves all it’s data in a store, so you would want to subscribe and react based on what changes you need to react to. As an example: ( () => … Read more

What values can we use in `formattingControls` parameter in RichText component

So what is the complete list of supported values? The list of available format types is maintained in the core/rich-text store. You can query the store by running this code in your browser’s console, which will return an array containing the format types: wp.data.select( ‘core/rich-text’ ).getFormatTypes(); And as of writing, there are 10 default/core format … Read more

@wordpress/components Button variants not styled

The WordPress components docs are pulled from the development branch of the repository, meaning they may describe features and functionality that have yet to be released. With the current version of the library used in WordPress, you declare the variant with the isPrimary, isSecondary, or isTertiary property: <Button isSecondary onClick={open}> Select image… </Button>

How to display InnerBlocks in render callback

save (JS) By default, the save function outputs the content using this as return: <InnerBlocks.Content /> This part is ok, you should still return <InnerBlocks.Content /> in your save function: save: props => <InnerBlocks.Content /> Source If you are using InnerBlocks in a dynamic block you will need to save the InnerBlocks in the save … Read more