Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snippet: How to lock blocks and prevent your users from unlocking them #344

Open
bph opened this issue Nov 21, 2024 · 0 comments
Open

Snippet: How to lock blocks and prevent your users from unlocking them #344

bph opened this issue Nov 21, 2024 · 0 comments

Comments

@bph
Copy link
Collaborator

bph commented Nov 21, 2024

Discussed in #342

Originally posted by ndiego November 18, 2024
This snippet would provide a very brief overview of the Block Locking API and then show developers how to prevent unlocking in specific contexts:

Post types:

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
    if ( $context->post && 'page' === $context->post->post_type ) {
        $settings['canLockBlocks'] = false;
    }

    return $settings;
}, 10, 2 );

User permissions:

add_filter( 'block_editor_settings_all', function( $settings ) {
    $settings['canLockBlocks'] = current_user_can( 'edit_theme_options' );

    return $settings;
} );

The snippet will also show how to disable access to the Code Editor, so users cannot manipulate the attributes directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

2 participants