Gutenberg: Restrict Top Level Blocks, But Not Child Blocks

Background

I’ve created a custom top level “page section” block, to work with my existing theme. I’ve like to restrict top level blocks to ONLY that one block. However, I don’t want to disable all blocks completely, because I want all blocks available to use as innerBlocks.

Question

How can I restrict top level blocks, without restricting child level blocks?

1 Answer
1

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';
}

Leave a Comment