|

WordPress Block Locking — with permissions

Want to control who can lock and unlock blocks in WordPress?

In Full Site Editing, editors can change key layouts like headers and footers, which isn’t always ideal. In a recent project, I needed to control this, and fortunately, WordPress provides a solution with the block_editor_settings_all filter.

TL;DR;

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

You can lock or unlock blocks by their style or unique ID. I also like setting up custom permissions for headers, footers, and specific content blocks. It’s a great way to control who can edit different parts of the site.

Restrict Block Locking Based on User Capabilities

Another way is to restrict block locking to users who can edit the site’s design. You can do this by checking if the user has the edit_theme_options permission:

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

This code ensures only users with theme editing access can lock or unlock blocks.

Targeting Specific Blocks with Styles

Sometimes, you want to target specific blocks and apply locking permissions when a certain block style is used. This keeps custom-styled sections or reusable blocks consistent. Here’s how to do it:

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
    if ( $context->post && has_blocks( $context->post->post_content ) ) {
        $blocks = parse_blocks( $context->post->post_content );

        foreach ( $blocks as $block ) {
            if ( isset( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'is-style-fancy-header' ) !== false ) {
                $settings['canLockBlocks'] = false;
            }
        }
    }
    return $settings;
}, 10, 2 );

This code disables locking for any block with the “fancy-header” style, ensuring it remains consistent across your site.

Locking Specific Blocks by Block ID

For more precise control, you can lock specific blocks based on their unique ID. Here’s an example:

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
    $locked_block_ids = array( 'block-id-123', 'block-id-456' );

    if ( $context->post && has_blocks( $context->post->post_content ) ) {
        $blocks = parse_blocks( $context->post->post_content );

        foreach ( $blocks as $block ) {
            if ( isset( $block['attrs']['id'] ) && in_array( $block['attrs']['id'], $locked_block_ids ) ) {
                $settings['canLockBlocks'] = false;
            }
        }
    }
    return $settings;
}, 10, 2 );

For more control, you can create custom permissions that let only certain users edit headers and footers. Here’s how:

Register Custom Capabilities:

function add_custom_capabilities() {
    $role = get_role( 'editor' );

    if ( $role ) {
        $role->add_cap( 'edit_header' );
        $role->add_cap( 'edit_footer' );
    }
}
add_action( 'init', 'add_custom_capabilities' );

Restrict Editing Based on Capabilities:

add_filter( 'block_editor_settings_all', function( $settings ) {
    if ( ! current_user_can( 'edit_header' ) && is_header_block() ) {
        $settings['canLockBlocks'] = true;
    }

    if ( ! current_user_can( 'edit_footer' ) && is_footer_block() ) {
        $settings['canLockBlocks'] = true;
    }

    return $settings;
} );

Here, is_header_block() and is_footer_block() are custom functions that check if a block is in the header or footer.

Custom Permissions for Post Content Editing

You can create custom permissions that let editors edit post content only when specific blocks have a custom setting.

Add Custom Capability:

function add_custom_content_capability() {
    $role = get_role( 'editor' );

    if ( $role ) {
        $role->add_cap( 'edit_locked_content' );
    }
}
add_action( 'init', 'add_custom_content_capability' );

Lock Blocks Based on Custom Settings:

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
    if ( $context->post && has_blocks( $context->post->post_content ) ) {
        $blocks = parse_blocks( $context->post->post_content );

        foreach ( $blocks as $block ) {
            if ( isset( $block['attrs']['lockToUserGroup'] ) && ! current_user_can( 'edit_locked_content' ) ) {
                $settings['canLockBlocks'] = true;
            }
        }
    }
    return $settings;
}, 10, 2 );

This code locks blocks based on a custom attribute (lockToUserGroup), ensuring that only users with specific permissions can edit them.

In Short

  • Locking by Block ID: Use the block_editor_settings_all filter to lock specific blocks based on their ID.
  • Custom Permissions for Header/Footer: Create custom capabilities like edit_header and edit_footer and use them to restrict access to these areas.
  • Custom Post Content Permissions: Add custom block settings and user capabilities to control who can edit specific content blocks.

With these steps, you can control how blocks are locked or unlocked in the WordPress editor. This gives you precise control over who can edit content and layouts based on user roles and needs.

Share your thoughts

Your email address will not be published. Required fields are marked *

Latest Articles