Using WordPress ‘allowed_block_types’ PHP filter

The allowed_block_types WordPress PHP filter lets you control the block types available for the editor.

Usage

add_filter('allowed_block_types', 'your_function_name', 10, 2);

function your_function_name($allowed_block_types, $post) {
    // your custom code here
    return $allowed_block_types;
}

Parameters

  • $allowed_block_types: bool|string[] — Array of block type slugs, or boolean to enable/disable all. Default true (all registered block types supported).
  • $post: WP_Post — The post resource data.

More information

See WordPress Developer Resources: allowed_block_types

Examples

Restrict block types for a specific post type

function restrict_block_types_for_post_type($allowed_block_types, $post) {
    if ($post->post_type == 'custom_post_type') {
        $allowed_block_types = array('core/paragraph', 'core/image');
    }
    return $allowed_block_types;
}
add_filter('allowed_block_types', 'restrict_block_types_for_post_type', 10, 2);

Disable all blocks except specific ones

function disable_all_blocks_except_specific($allowed_block_types, $post) {
    $allowed_block_types = array('core/paragraph', 'core/heading');
    return $allowed_block_types;
}
add_filter('allowed_block_types', 'disable_all_blocks_except_specific', 10, 2);

Enable all blocks for a specific user role

function enable_all_blocks_for_role($allowed_block_types, $post) {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
        $allowed_block_types = true;
    }
    return $allowed_block_types;
}
add_filter('allowed_block_types', 'enable_all_blocks_for_role', 10, 2);

Disable a specific block type for a category

function disable_specific_block_for_category($allowed_block_types, $post) {
    if (has_category('restricted-category', $post)) {
        $disabled_block = 'core/video';
        $allowed_block_types = array_diff($allowed_block_types, array($disabled_block));
    }
    return $allowed_block_types;
}
add_filter('allowed_block_types', 'disable_specific_block_for_category', 10, 2);

Disable all block types

function disable_all_block_types($allowed_block_types, $post) {
    return false;
}
add_filter('allowed_block_types', 'disable_all_block_types', 10, 2);