By adding a new post, we have access to all core blocks from the very beginning. I would like to limit this and prevent some core blocks from being used on the main level. They should only be available inside my custom inner block.
I have been given some advice about:
- using the
template
with init
action
- using the
parent
attribute in custom block
but all this does not limit the availability of blocks at the main level
I figured maybe hiding blocks, depending on where the inserter is in the DOM
structure might be an idea but I’m not sure it’s a good direction.
You will need to filter the template for the post type:
add_action( 'init', 'setup_template' );
function setup_template() {
$post_object = get_post_type_object( 'post' );
$post_object->template = [ [ 'your/custom/block' ] ];
$post_object->template_lock = 'all';
}
This will pre-populate the post with a single instance of your custom block and lock it from being changed.
By locking it, they cannot insert any top-level blocks but can still insert items into your custom block. There is more info in the docs about locking, you may need insert
instead of all
depending on your intent.
Hope it helps.