WordPress 5.9 will introduce a new API for Lock Blocks – WP Tavern
The advent of block themes offers more creative power to users, but there are times when theme authors want to lock down key elements of a design and its designated content areas. First introduced in Gutenberg 11.6, the upcoming WordPress 5.9 release will include a new API for locking blocks.
Template-level locking has been available in Gutenberg for a few years, allowing developers to lock the template to the UI so that users cannot manipulate the blocks. This new API provides more granular control that can be applied at the block level and replace model locking.
“Instead of applying a lock to all internal blocks, you can selectively apply it to individual blocks via the
lock attribute,” Marcus Kazmierczak said in the dev note. “Block-level locking would override legacy locking
templateLock value. You can choose to lock moving or deleting a block.
One of the main use cases for locking individual blocks, cited in the ticket proposing the new API, is where one can lock the “post-content” block of a
single template so that users cannot delete it.
“Another use case we’re building is to have a payment block with different blocks that act as fundamental steps, we don’t want people to delete or move those steps because they’re fundamental and their order is also important , but we want to allow people to select them, access settings, and insert blocks between them,” said Seghir Nadir, WooCommerce Engineer.
Kazmierczak’s dev note shows how developers can lock a specific block in a model and explains how block-level locking is not inheritable.
If a block is locked against deletion, its children can still be deleted. Kazmierczak said. “If you also want to apply the lock to children, add
templateLock to the internal block component, or
templateLock attribute to support blocks.
For more information on the new locking mechanism, see the block editor manual and the code samples in the dev note.