Support Forum
Support › MB Blocks › Possible to template_lock MB Block and still allow other blocks to be added?Resolved
Hello!
I've created a block in MB, and have set the following options:
storage_type = post_meta
multiple = false
In the post type, I set the following options:
'template' => array(
array( 'core/paragraph' ),
array( 'meta-box/test' ),
),
'template_lock' => 'all',
I only want to lock my meta-box/test block, but still allow other core blocks to be added to support full formatting capabilities and saved in the post_content field. How would I accomplish this?
Thanks!
Jake
Hello Jacob,
It looks like you want to add one instance of MB test block on a post/page and allow other blocks added. Please use the setting supports
with multiple
set to false
.
'supports' => [
// Use the block just once per post
multiple: false,
],
Hi Peter! Thanks for the response!
I'm actually trying to create a template, and lock the meta-box/test block and prevent it from being removed, but allow other blocks to be added.
I looked into individual blocking, but wasn't able to get it working: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-templates/#individual-block-locking
add_action( 'init', 'your_prefix_register_post_type' );
function your_prefix_register_post_type() {
...
'template' => array(
array( 'core/paragraph' ),
// Allow a Paragraph block to be moved or removed.
array( 'meta-box/test', array(
'placeholder' => 'Add Description...',
// "template_lock" => 'all',
'lock' => array(
'move' => true,
'remove' => true,
),
) ),
),
...
Let me know if you have a solution. Thank you!
Thanks for sharing the info.
The attribute lock
does not look supported when registering the custom block. I will inform the development team to consider supporting this feature in future updates.
Thanks very much, Peter!
Hi Jacob,
I've just tested locking a custom block created with MB Blocks and it works. Here is a sample code taken from the WP docs, that adds 2 blocks to the post default template and locks them from removing and moving (this is block-level locking).
add_action( 'init', function () {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = [
[
'core/paragraph',
[
'placeholder' => 'Content goes here....',
'lock' => [
'move' => true,
'remove' => true,
],
],
],
[
'meta-box/testimonial',
[
'lock' => [
'remove' => true,
'move' => true,
],
],
],
];
} );
And here is the screenshot:
https://monosnap.com/direct/PWSTrtDMnvpYHhU1lxPHjNiZquXKYg
(The code will work similarly when you register_post_type
, here the post
post type is registered, so I just get the post type object).
In addition, if you want to remove the locking UI (the lock icon), which helps preventing from unlock the block, you can add lock
to the list of block supports
, like this:
<?php
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Testimonial',
'id' => 'testimonial',
'type' => 'block',
'render_template' => __DIR__ . '/testimonial-template.php',
'supports' => [
'lock' => false, // THIS
],
'fields' => [
[
'type' => 'text',
'id' => 'name',
'name' => 'Name',
],
[
'type' => 'textarea',
'id' => 'content',
'name' => 'Content',
],
[
'type' => 'single_image',
'id' => 'image',
'name' => 'Image',
],
],
];
return $meta_boxes;
} );