Discover in this tutorial how to prevent a user from modifying blocks that you have locked in certain WordPress post type.
With the arrival of the block and site editor (Site Editing) to WordPress, one of the concerns of designers and developers was that the end user has too much control over the design and can “disrupt” the work of the professional.
That’s why in WordPress 5.9 one of the features that arrived was the ability to lock blocks. Thanks to the lock
attribute you can control this locking, specifically with two options:
remove
: controls whether the block can be deleted or not.move
: controls whether the block can be moved or not.
When the option setting is true
it applies the lock.
This would be the HTML markup of a paragraph block in which both the move and delete options are locked:
<!-- wp:paragraph {"lock":{"move":true,"remove":true}} -->
<p>Heading two</p>
<!-- /wp:paragraph -->
With WordPress 6.0 version came the user interface that allows you to make these changes visually. The option is in the block menu:
By default, all blocks are created with the locking options disabled:
And the user can enable or disable them by clicking on the options:
Locked blocks will show a lock icon:
Having a user interface to be able to lock blocks is all very well, but if all users can modify and unlock them, it stops fulfilling its main purpose, don’t you think?
In a previous tutorial I explained how you can lock this interface so that it is only accessible by user role so that, for example, only administrators have access.
In this one I will explain how to do it for a certain type of entry (post, pages, CPT entries…). This way, you make sure that the client does not delete or move blocks that it should not.
Let’s see how you can achieve this.
Hide the block blocking interface on a certain post type
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this code you use the block_editor_settings_all
filter in conjunction with the current_user_can
function to not display the block blocking interface on pages.
Once applied, when users go to edit a page they will not see the option to lock, move or delete:
They will also not see the padlock on the locked blocks, nor the option to move up or down the block:
If you want to apply it to another type of entry you only have to modify the code. For example, this would be the snippet you would have to use if you want to limit it to the course CPT entries:
To see this and another 1097 code snippets of this website, login or subscribe here.
Conclusion
Now you know how to lock some blocks of a certain type of entry (post, pages, CPT entries…) and prevent the client from modifying or deleting them by mistake.
This allows you to give the client access to modify the content without losing the structure you have created.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉