How to Control Allowed Blocks in Nested InnerBlocks
Content
One of the more powerful features of the WordPress block editor is the ability to create nested block structures using InnerBlocks. However, a common point of confusion arises when trying to control which blocks are allowed at different levels of this hierarchy. This guide will explain the expected behavior and provide solutions for properly restricting blocks in parent and child blocks.
The Problem: Inheritance of Allowed Blocks
Many developers expect that setting the allowedBlocks property on a parent block will only affect that block's direct children. However, as noted in the community forums, a parent block's allowedBlocks setting can sometimes affect its grandchildren and deeper nested blocks as well. This can be problematic if you want a parent block to only allow a specific child block, but then want that child block to be able to contain a wider variety of grandchild blocks.
Why This Happens
This behavior is part of the block editor's design. The allowedBlocks property defines which blocks can be inserted into the InnerBlocks area it is applied to. By default, this setting is inherited by any nested InnerBlocks within the allowed blocks unless those nested blocks explicitly define their own allowedBlocks list.
Solution: Define Allowed Blocks at Each Level
The most effective solution is to explicitly define the allowedBlocks for each level of your nested block structure. According to the official WordPress documentation on Nested Blocks and InnerBlocks, different blocks can define their own allowed blocks.
Here's how to implement this:
- Parent Block: In your parent block's
editfunction, set theallowedBlocksprop on its InnerBlocks component to an array containing only your custom child block, e.g.,['namespace/child-block']. - Child Block: In your child block's
editfunction, define its own InnerBlocks component with a separateallowedBlockslist. This list should contain all the blocks you want to allow within the child block. This explicit definition will override any inheritance from the parent.
Example Code Snippet for a Child Block:
// Inside the child block's edit function
<InnerBlocks
allowedBlocks={['core/paragraph', 'core/image', 'core/heading']}
/>
Alternative Approach: Using a Block Template
For more complex or rigid structures, you can use a block template with the InnerBlocks component. A template can lock the initial block structure, preventing users from removing blocks or changing the order. While this is more restrictive, it ensures the layout remains exactly as intended.
Important Considerations
- This approach requires that your child block is a custom block you have created, as you need access to its source code to modify its InnerBlocks properties.
- Testing is crucial. After implementing these changes, verify that blocks can be inserted as expected at each level of your structure and that the parent's restrictions do not inadvertently affect deeper nesting.
- As highlighted in community discussions, the visibility of blocks in the slash inserter menu (
/) when some are disallowed can be a separate UI issue. The focus of this solution is on the functional restriction of block insertion.
By carefully defining allowedBlocks at each level of your nested block structure, you can achieve the precise control needed for complex layouts without unintended inheritance issues.
Related Support Threads Support
-
A way to do a Persistent or toggle view of BLOCK LIST?https://wordpress.org/support/topic/a-way-to-do-a-persistent-or-toggle-view-of-block-list/
-
Removing white space between…. items in a block? or is it a block?https://wordpress.org/support/topic/removing-white-space-between-items-in-a-block-or-is-it-a-block/
-
Replacement for DimensionControl: Component for setting dimensions?https://wordpress.org/support/topic/replacement-for-dimensioncontrol-component-for-setting-dimensions/
-
How to add additional blocks to the Navigation blockhttps://wordpress.org/support/topic/how-to-add-additional-blocks-to-the-navigation-block/
-
Hide Block from inserter sidebar, show in toolbarhttps://wordpress.org/support/topic/hide-block-from-inserter-sidebar-show-in-toolbar/
-
Insert Synced Pattern/Reusable with Titlehttps://wordpress.org/support/topic/insert-synced-pattern-reusable-with-title/
-
Block Binding API – Add prefix text with the binding valuehttps://wordpress.org/support/topic/block-binding-api-add-prefix-text-with-the-binding-value/
-
Issues with AllowedBlocks not letting core/navigation-link be insertedhttps://wordpress.org/support/topic/issues-with-allowedblocks-not-letting-core-navigation-link-be-inserted/
-
Excerpt Block (Since 6.6)https://wordpress.org/support/topic/excerpt-block-since-6-6/
-
allowedBlocks affecting nested blocks?https://wordpress.org/support/topic/allowedblocks-affecting-nested-blocks/
-
WordPress nested block variationhttps://wordpress.org/support/topic/wordpress-nested-block-variation/
-
How to Restrict allowedBlocks in InnerBlocks Based on Parent Block?https://wordpress.org/support/topic/how-to-restrict-allowedblocks-in-innerblocks-based-on-parent-block/
-
Link a dynamic attribute from wordpress database to Site Editor in WordPress Blohttps://wordpress.org/support/topic/link-a-dynamic-attribute-from-wordpress-database-to-site-editor-in-wordpress-blo/
-
Reduce the space between 2 blockshttps://wordpress.org/support/topic/reduce-the-space-between-2-blocks/
-
Help with Displaying Background Image in Core Column Block Editorhttps://wordpress.org/support/topic/help-with-displaying-background-image-in-core-column-block-editor/
-
How to prevent ‘third-party available blocks’ in block inserter…https://wordpress.org/support/topic/how-to-prevent-third-party-available-blocks-in-block-inserter/
-
AllowedBlocks filter should also hide disallowed blocks from slash inserterhttps://wordpress.org/support/topic/allowedblocks-filter-should-also-hide-disallowed-blocks-from-slash-inserter/
-
Checking if block is present anywhere in the DOM?https://wordpress.org/support/topic/checking-if-block-is-present-anywhere-in-the-dom/
-
Block Editor Inserterhttps://wordpress.org/support/topic/block-editor-inserter/
-
Prevent block variations and filters from polluting the entire block editorhttps://wordpress.org/support/topic/prevent-block-variations-and-filters-from-polluting-the-entire-block-editor/
-
Root BlockAppenderhttps://wordpress.org/support/topic/root-blockappender/
-
Detect if in Block Editor or nothttps://wordpress.org/support/topic/detect-if-in-block-editor-or-not/
-
Default HTML element for the WordPress group blockhttps://wordpress.org/support/topic/default-html-element-for-the-wordpress-group-block/
-
Limit allowedFormats for custom block that uses InnerBlockshttps://wordpress.org/support/topic/limit-allowedformats-for-custom-block-that-uses-innerblocks/
-
Allowing my custom block as inner block for core/paragraphhttps://wordpress.org/support/topic/allowing-my-custom-block-as-inner-block-for-core-paragraph/
-
Remove height and width inputs (“Image Dimensions”) from core/imagehttps://wordpress.org/support/topic/remove-height-and-width-inputs-image-dimensions-from-core-image/
-
UI Dropdown list with ADD an item featurehttps://wordpress.org/support/topic/ui-dropdown-list-with-add-an-item-feature/
-
Detecting if block is being rendered inside componenthttps://wordpress.org/support/topic/detecting-if-block-is-being-rendered-inside-component/
-
Always show block margins/guideshttps://wordpress.org/support/topic/always-show-block-margins-guides/
-
Extending core blocks – Add a custom control UI to core group ‘ROW’https://wordpress.org/support/topic/extending-core-blocks-add-a-custom-control-ui-to-core-group-row/
-
Allow editor to use IFRAME code only but disable javascript code?https://wordpress.org/support/topic/allow-editor-to-use-iframe-code-only-but-disable-using-javascript/
-
RichText component sanitizationhttps://wordpress.org/support/topic/richtext-component-sanitization/
-
Need to know what block template is rendering inner blockshttps://wordpress.org/support/topic/need-to-know-what-block-template-is-rendering-inner-blocks/
-
Extra block is auto addedhttps://wordpress.org/support/topic/extra-block-is-auto-added/