Skip to content

Instantly share code, notes, and snippets.

@ryanwelcher
Last active July 19, 2022 09:35
Show Gist options
  • Save ryanwelcher/4cf6053508bd0e3292d660812bfcfa86 to your computer and use it in GitHub Desktop.
Save ryanwelcher/4cf6053508bd0e3292d660812bfcfa86 to your computer and use it in GitHub Desktop.
WordPress 6.0: Examples on how to restrict block locking in the UI
<?php
/**
* Examples on restricting when blocks can be locked using the UI.
* Based on the example provided by @mamaduka in https://github.com/WordPress/gutenberg/pull/39566
*
* @see https://developer.wordpress.org/reference/hooks/block_editor_settings_all/
*/
add_filter(
'block_editor_settings_all',
function( $settings, $context ) {
// Allow for the Editor role and above - https://wordpress.org/support/article/roles-and-capabilities/.
$settings['canLockBlocks'] = current_user_can( 'delete_others_posts' );
// Only enable for specific user(s).
$user = wp_get_current_user();
if ( in_array( $user->user_email, array( 'username@example.com' ), true ) ) {
$settings['canLockBlocks'] = false;
}
// Disable for posts/pages.
if ( $context->post && 'page' === $context->post->post_type ) {
$settings['canLockBlocks'] = false;
}
return $settings;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment