sinanisler logo

Disabling Blocks in WordPress with PHP

Only allow Heading, Image, List, and Paragraph blocks

function allow_specific_blocks( $allowed_block_types, $block_editor_context ) {
    return array(
        'core/heading',
        'core/image',
        'core/list',
        'core/paragraph',
    );
}
add_filter( 'allowed_block_types_all', 'allow_specific_blocks', 10, 2 );

Allows more blocks for Admins or Editors and restricts other users

function allow_blocks_for_specific_roles( $allowed_block_types, $block_editor_context ) {
    if ( ! current_user_can( 'publish_pages' ) ) {
        return array(
            'core/heading',
            'core/image',
            'core/list',
            'core/paragraph',
        );
    }
    return true; // Allow all blocks for users who can publish pages.
}
add_filter( 'allowed_block_types_all', 'allow_blocks_for_specific_roles', 10, 2 );

For More: https://developer.wordpress.org/news/2024/01/29/how-to-disable-specific-blocks-in-wordpress/

Leave the first comment