5

Is there any way to force a Gutenberg block to be on every page of a post type?

I tried to add a template on my page and it does almost what I need, but I can still remove my default blocks.

The closest I've come is this:

function ckt_mina_init_required_block()
{
    // create a new page template (pages only)
    $page_type_object = get_post_type_object('page');
    $page_type_object->template = [
        [
            'core/group',
            [],
            [
                // add a paragraph block for the page summary
                [
                    'core/paragraph',
                    [
                        'placeholder' => __('Excerpt'),
                    ],
                ],
                [
                    'core/image',
                    [],
                ],
            ],
        ],
    ];
    $page_type_object->template_lock = 'all';
}

add_action('init', 'ckt_mina_init_required_block');

This will create a new page template and use it on every new page. The 2 blocks "paragraph" and "image" will be present in the first group.

But I cannot add any other block to my page.

Or, I can remove the template_lock = 'all', but then, my blocks can be removed during the page creation.

Any idea on how I might achieve to have 2 fixed blocks at the beginning of my Gutenberg area?

2
  • what problem does this solve? This looks like something that would be solved via full site editing, but you've implied this is actually something the user can change on a per post basis. Is this an editorial requirement?
    – Tom J Nowell
    Jan 28, 2022 at 2:30
  • 1
    The goal is to force the user to always set those two blocks at the top of the page content to keep consistency in the page design.
    – Tim
    Jan 28, 2022 at 6:30

2 Answers 2

1

There is a lock feature that can be toggled in the blocks toolbar which is marked up like this. I'm not sure in what version this feature was implemented.

<!-- wp:paragraph {"lock":{"move":true,"remove":true}} -->
<p>C1</p>
<!-- /wp:paragraph -->

To lock your excerpt paragraph I would do this.

[
    'core/paragraph',
    [
        'placeholder' => __('Excerpt'),
        'lock' => [
            'move' => true,
            'remove' => true
        ]
    ],
]
0

No fast and easy solution for you. Read discussion here and here.

You may try to write some rather sophisticated Gutenberg code to implement this feature, or maybe you could implement some save_post hook to check saved block structure with parse_blocks and do something if they don't match your required structure. For example, prevent post publication or insert required blocks into content.

1
  • 2
    Yep, that's the kind of threads I saw. Only a few people interested so far and no promise that it will be implemented anytime soon. I'll try and do my best to achieve what I need and will post the result here.
    – Tim
    Feb 21, 2020 at 15:50

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.