Using WordPress ‘get_allowed_block_types()’ PHP function

The get_allowed_block_types() WordPress PHP function retrieves the list of allowed block types to use in the block editor.

Usage

get_allowed_block_types( $block_editor_context );

Example:

Input:

$allowed_block_types = get_allowed_block_types( $block_editor_context );

Output:

Array of allowed block types

Parameters

  • $block_editor_context (WP_Block_Editor_Context) – Required. The current block editor context.

More information

See WordPress Developer Resources: get_allowed_block_types()

Examples

Get allowed block types

Retrieve the allowed block types for the current block editor context.

$block_editor_context = new WP_Block_Editor_Context();
$allowed_block_types = get_allowed_block_types( $block_editor_context );

// Display allowed block types
foreach ( $allowed_block_types as $block_type ) {
    echo 'Block Type: ' . $block_type . '<br>';
}

Filter allowed block types

Limit allowed block types to a specific list.

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

function my_allowed_block_types( $allowed_block_types, $block_editor_context ) {
    // Only allow 'core/paragraph' and 'core/image' block types
    return array( 'core/paragraph', 'core/image' );
}

Disable all block types

Disable all block types from the block editor.

add_filter( 'allowed_block_types', '__return_false' );

Allow all block types

Allow all block types in the block editor.

add_filter( 'allowed_block_types', '__return_true' );

Custom block type restriction

Only allow specific block types for posts in a certain category.

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

function my_custom_allowed_block_types( $allowed_block_types, $block_editor_context ) {
    // Get the post ID from the block editor context
    $post_id = $block_editor_context->post->ID;

    // Check if the post is in the 'restricted' category
    if ( has_category( 'restricted', $post_id ) ) {
        // Only allow 'core/paragraph' block type
        return array( 'core/paragraph' );
    }

    return $allowed_block_types;
}