7

In a WordPress plugin I have created some custom 'layout' Gutenberg blocks. These are basically 'boxes' that contain the rest of the page contents. I'd like to restrict the user to adding only these boxes into a page, but then allowing them to place ANY child blocks within them.

I have found how to limit Gutenburg blocks using the allowed_block_types filter. This works to just restrict the user to adding 'boxes' to a page.

I have then found how to limit a Gutenberg block to only allowing specific child blocks. i.e. on InnerBlocks, specify allowedBlocks: ['core/paragraph','core/list','core/seperator',...] so that the 'boxes' can contains these child blocks.

The problem is that the allowed_block_type filter seems to override the allowedBlocks.

How can I allow specific blocks at 'page' level, and others at the 'child' level?

Projectitis
  • 303
  • 1
  • 6

1 Answers1

2

Crossposted from my answer on WordPress StackExchange

The short answer is you can’t. But you can accomplish this by using a block template that only contains your block and is locked. If your block has an InnerBlocks instance, you can add any of the registered blocks to it.

add_action( 'init', 'insert_template' );
function insert_template() {
    $post_type_object = get_post_type_object( 'post' );
    $post_type_object->template =[ [ 'your-custom-block-name'] ];
    $post_type_object->template_lock = 'all';
}
Welcher
  • 294
  • 1
  • 5