Create a custom render Appender button to add Inner Blocks

So I’m answering the following: How to open the block picker menu via a custom button. I.e. Without using InnerBlocks.ButtonBlockAppender. 🙂 How can I open the Block Picker Menu on click of the custom button? There’s no (as of writing) “standard” function (like alert()) that you can simply call which then opens the block picker … Read more

hardcrop images in gutenberg “latest posts” block

The Latest Posts block uses (wp.data.select( ‘core/block-editor’ ).getSettings() to get) the image sizes in the editor settings which can be filtered via the block_editor_settings hook in PHP: apply_filters( ‘block_editor_settings’, array $editor_settings, WP_Post $post ) Filters the settings to pass to the block editor. And the setting name used for the image sizes is imageSizes. So … Read more

How do you render_callback for register_block_type to a method in another class?

Option 1 is the solution, here’s a smaller example: $obj = new Obj(); …. register_block_type( ‘my-plugin/my-block’, [ ‘editor_script’ => ‘editor-script-handle’, ‘render_callback’ => [ $obj, ‘block_save_function’ ] ] ); In that code [ $obj, ‘block_save_function’ ] is equivalent to $obj->block_save_function(…. The important thing to note, is that render_callback‘s value is a PHP callable type. You need … Read more

How do I access site and block editor state data and use `useSelect()` or `withSelect()` to bind it to my components?

✨ Intro to the Gutenberg Data Module ✨ Gutenberg’s core data model shares a lot of the API and ideology of the state-management library Redux, with a number of differences. Reading up on Redux can provide a lot of insight into how Gutenberg’s data management functions. This lecture will be on the final exam. Application … Read more

Pre-set CSS class into custom block/template

In case anyone is looking for an answer to this that doesn’t involve adding a filter and a bunch of extra code, you can add a className to the block directly in the template code. This is the same code that the OP used, with one small adjustment. “class” becomes “className”: function myplugin_register_book_post_type() { $args … Read more

How to get value of selected page template in Gutenberg editor?

I slightly modified SkyShab’s code, because it fired template change event on page load and it didn’t fire when template was changed to default (since getEditedPostAttribute( ‘template’ ) is then ”, which equals to null when testing for template change) const { select, subscribe } = wp.data; class PageTemplateSwitcher { constructor() { this.template = null; … Read more

Why is my Gutenberg block returning an error after initial save?

The main issue in your code is with these two attributes which use the exact same selector (the first span element in your block content): checkListItemOne: { type: ‘string’, source: ‘html’, selector: ‘span’ // same as below }, checkListItemTwo: { type: ‘string’, source: ‘html’, selector: ‘span’ // same as above } Which means the block … Read more

Add a button to the toolbar of an existing Gutenberg block

Edit: WordPress 5.9 or above. WordPress 5.9 moved the slotfill behind the useDisplayBlockControls hook which prevents fills from registering unless they are within the currently selected block’s component. It is no longer possible to use the BlockControls slotfill for adding elements to toolbars of WordPress core blocks. Original: WordPress 5.8.3 or below. There is an … Read more