Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Gutenberg blocks: How to restrict page-level blocks, but allow all child-level blocks

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?

like image 531
Projectitis Avatar asked May 03 '19 01:05

Projectitis


People also ask

How do I group Gutenberg blocks?

Grouping Existing Blocks First, select the block or blocks you wish to group. You can select multiple blocks by clicking and dragging or by holding down the “shift” key and clicking on each of them (they need to be next to each other). The block toolbar will appear. Select the Group icon.

What is visibility block?

Block Visibility Groups allows the site administrator to easily manage complex visibility settings that apply to any block placed in a visibility group. The visibility settings for all blocks in the group can be edited on one administration form.


1 Answers

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';
}
like image 97
Welcher Avatar answered Oct 22 '22 04:10

Welcher