0

I am trying to create a new group block that includes one core/heading block and then one core/gallery block. I do not want editors to be able to add other inner blocks to the group. Nor do I want editors to be able to delete either of the heading or gallery blocks.

Here is my block variation so far:

    wp.blocks.registerBlockVariation("core/group", {
        name: "logo-grid",
        title: "Logo Grid",
        description: "",
        category: "media",
        icon: "smiley",
        scope: ["inserter"],
        attributes: {
        },
        innerBlocks: [
            ["core/heading", { level: 2, placeholder: "Heading" }],
            ["core/gallery"],
        ],
    });

I've looked into templateLock and template_lock and lock attributes but none seem to apply to block variations?

I realize I may be using the wrong tool for the job by trying to create a block variation with this functionality. Any advice for how to achieve what I want?

2 Answers 2

1

I think I've figured this out. Instead of creating a block variation, I just created a brand new block using a block template.

import { registerBlockType } from "@wordpress/blocks";
import { useBlockProps, InnerBlocks } from "@wordpress/block-editor";

const BLOCKS_TEMPLATE = [
    ["core/heading", { level: 2, placeholder: "Heading" }],
    ["core/gallery", {}],
];

registerBlockType("logo-grid", {
    apiVersion: 2,
    title: "Logo Grid",
    icon: "smiley",
    category: "media",
    edit: () => {
        return (
            <div {...useBlockProps()}>
                <InnerBlocks
                    orientation="horizontal"
                    template={BLOCKS_TEMPLATE}
                    templateLock="all"
                />
            </div>
        );
    },
    save: () => {
        return (
            <div {...useBlockProps.save()}>                
                <InnerBlocks.Content />               
            </div>
        );
    },
});
1
0

It's possible that this has changed since the original post, but I am able to add template locking to a block variation as follows in WordPress 6.2.2 by adjusting the attributes of the block variation.

For locking removal and movement:

    wp.blocks.registerBlockVariation("core/group", {
        name: "logo-grid-lock",
        title: "Logo Grid Locked",
        description: "",
        category: "media",
        icon: "smiley",
        scope: ["inserter"],
        attributes: {
            lock: {
                move: 'true',
                remove: 'true',
            }
        },
        innerBlocks: [
            ["core/heading", { level: 2, placeholder: "Heading" }],
            ["core/gallery"],
        ],
    });

Using the even more restrictive content-editing-only "template lock":

    wp.blocks.registerBlockVariation("core/group", {
        name: "logo-grid-templatel-locked",
        title: "Logo Grid Template Locked",
        description: "",
        category: "media",
        icon: "smiley",
        scope: ["inserter"],
        attributes: {
            templateLock: 'contentOnly'
        },
        innerBlocks: [
            ["core/heading", { level: 2, placeholder: "Heading" }],
            ["core/gallery"],
        ],
    });

Locking Blocks in WordPress 5.9 Content only editing and other locking updates

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.