Possible to template_lock MB Block and still allow other blocks to be added?
Support › MB Blocks › Possible to template_lock MB Block and still allow other blocks to be added?Resolved
- This topic has 6 replies, 3 voices, and was last updated 2 years, 4 months ago by
Anh Tran.
-
AuthorPosts
-
November 2, 2022 at 8:02 PM #38935
Jacob Hill
ParticipantHello!
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
November 3, 2022 at 10:11 PM #38951Peter
ModeratorHello 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
withmultiple
set tofalse
.'supports' => [ // Use the block just once per post multiple: false, ],
November 5, 2022 at 10:55 AM #38963Jacob Hill
ParticipantHi 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!
November 7, 2022 at 10:57 PM #38989Peter
ModeratorThanks 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.November 8, 2022 at 12:10 AM #38991Jacob Hill
ParticipantThanks very much, Peter!
December 2, 2022 at 4:08 PM #39454Anh Tran
KeymasterHi 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 thepost
post type is registered, so I just get the post type object).December 2, 2022 at 4:11 PM #39455Anh Tran
KeymasterIn 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 blocksupports
, 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; } );
-
AuthorPosts
- You must be logged in to reply to this topic.