Inner blocks
When creating custom blocks in PorterWP, you can easily define default templates and allowed inner blocks directly in your block's template.php
file. This allows you to control what blocks are allowed within your custom block and what default content should be preloaded when a user inserts the block.
Example: The Default template.php
template.php
The basic structure of a block's template.php
file might look like this:
This code uses the $allowed_blocks
array to define which blocks are allowed to be inserted inside your custom block, and the $template
array to set default inner block content.
Example: Defining Allowed Blocks and Default Template
Here’s an example where you specify the allowed blocks and set up a default template:
Key Concepts:
Allowed Blocks (
$allowed_blocks
): This array defines the block types that are permitted to be added inside your custom block. In the example above, onlycore/paragraph
andacf/meta
blocks are allowed.Default Template (
$template
): This array defines the default block(s) and attributes that are automatically inserted when the block is added to the page. In this case, acore/paragraph
block with a placeholder text "e.g. Applications open" is preloaded.Attributes for InnerBlocks:
allowedBlocks
: This attribute defines which blocks are allowed to be added inside your block.template
: This attribute specifies the default template for the block.templateLock
: SettingtemplateLock=false
allows the user to add, move, and remove blocks inside your custom block.
How It Works
In the above example, when a user inserts the "Badge" block:
The only blocks they can add are
core/paragraph
andacf/meta
.A default
core/paragraph
block with the placeholder "e.g. Applications open" is already present in the block when it is inserted.
By customizing the $allowed_blocks
and $template
arrays, you can fully control the inner content and user experience for your custom blocks.
This setup allows for powerful customization of your blocks and ensures that they can come with pre-defined structures, making it easier for content creators to use them.
Last updated