Build a Gutenberg Block with InnerBlocks and Block Templates

A Gutenberg block with InnerBlocks and a template

Some content has a fixed shape: a feature card is always an icon, a heading, a paragraph and a button. A custom block with InnerBlocks gives editors exactly that shape, filled with core blocks they already know, and the theme styles one predictable structure instead of whatever the editor happened to build.

Problem: Editors build feature cards from loose image, heading and button blocks, and each card ends up with a different structure and spacing.

Solution: Register a block with block.json whose edit component renders useInnerBlocksProps() with a template of the child blocks, limit the allowed blocks with allowedBlocks, lock the structure with templateLock, and save the children with useInnerBlocksProps.save().

block.json

{
    "$schema": "https://schemas.wp.org/trunk/block.json",
    "apiVersion": 3,
    "name": "ha/feature-card",
    "title": "Feature card",
    "category": "design",
    "icon": "star-filled",
    "textdomain": "textdomain",
    "supports": {
        "html": false,
        "color": { "background": true, "text": true },
        "spacing": { "padding": true }
    },
    "editorScript": "file:./index.js",
    "style": "file:./style-index.css"
}

apiVersion 3 (WordPress 6.3+) marks the block as ready for the iframed editor. The supports section gives the card colour and padding controls without any code. The file structure and the build with @wordpress/scripts: block.json explained.

The edit and save functions

// src/index.js
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import metadata from './block.json';

const TEMPLATE = [
    [ 'core/image', { sizeSlug: 'thumbnail', className: 'ha-feature-card__icon' } ],
    [ 'core/heading', { level: 3, placeholder: 'Feature title' } ],
    [ 'core/paragraph', { placeholder: 'What does this feature do?' } ],
    [ 'core/buttons', {}, [ [ 'core/button', { placeholder: 'Learn more' } ] ] ],
];

registerBlockType( metadata.name, {
    edit() {
        const blockProps = useBlockProps( { className: 'ha-feature-card' } );
        const innerBlocksProps = useInnerBlocksProps( blockProps, {
            template: TEMPLATE,
            allowedBlocks: [ 'core/image', 'core/heading', 'core/paragraph', 'core/buttons' ],
            templateLock: 'all',
        } );
        return <div { ...innerBlocksProps } />;
    },
    save() {
        const blockProps = useBlockProps.save( { className: 'ha-feature-card' } );
        return <div { ...useInnerBlocksProps.save( blockProps ) } />;
    },
} );

useInnerBlocksProps() puts the child blocks directly into the block’s wrapper, so the markup is one div with the children inside, without the extra wrapper of the older <InnerBlocks /> component. The template is inserted when the card is added.

How strict should it be

templateLock: 'all'             no blocks can be added, removed or moved
templateLock: 'insert'          blocks can be moved, not added or removed
templateLock: 'contentOnly'     only the text and images can be edited (WordPress 6.1+)
templateLock: false             the template is only a starting point

'all' keeps the structure fixed but still lets editors change texts, images and links. If some cards need a second paragraph, false with allowedBlocks keeps the choice of blocks limited without forbidding changes.

Register it in PHP

add_action( 'init', function () {
    register_block_type( __DIR__ . '/build/feature-card' );
} );

NOTE: The saved HTML contains the child blocks, so changing the save() function later makes existing cards “invalid” in the editor; add a deprecated entry when you change it. For output that must always reflect the current PHP code, a dynamic block is the alternative: a dynamic block with server-side rendering.