3

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

2

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';
}
6
  • I did a quick search and that may be exactly what I need. I'll investigate it further today.
    – Kelderic
    Nov 30, 2019 at 17:31
  • 1
    I’ll try and get some code samples together and update my answer.
    – Welcher
    Nov 30, 2019 at 17:32
  • I added a bounty to this same question over on StackOverflow. If you'd like to post this answer there, I'll give you the bounty. stackoverflow.com/q/55962177/2111901
    – Kelderic
    Dec 4, 2019 at 3:08
  • And I think what I need is two levels. A custom block called page body, or something, that is called in the template and locked. Then that has an innerBlocks locked to only my page section blocks, but unlimited. Thanks!
    – Kelderic
    Dec 4, 2019 at 3:10
  • Glad to help! I posted to the bounty as well
    – Welcher
    Dec 4, 2019 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.