apply_filters_deprecated( ‘allowed_block_types’, bool|string[] $allowed_block_types, WP_Post $post )

This hook has been deprecated. Use the {@see ‘allowed_block_types_all’} filter instead.

Filters the allowed block types for the editor.

Parameters

$allowed_block_typesbool|string[]
Array of block type slugs, or boolean to enable/disable all.
Default true (all registered block types supported)
$postWP_Post
The post resource data.

Source

$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );

Changelog

VersionDescription
5.8.0Use the 'allowed_block_types_all' filter instead.
5.0.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    When filtering for allowed block types, return the boolean for the post types you are not restricting. Otherwise, it won’t work as Nilambar’s example because $allowed_block_types is expecting an array or a boolean.
    This worked for me when filtering allowed blocks for a custom post type. You’ll want to substitute ‘myplugin’ with your namespace and the post types you’re filtering in the switch statement.

    function myplugin_allowed_block_types( $allowed_block_types, $post ) {
    	
    	switch( $post->post_type ) {
            case 'my_cpt_1':
                return array( 'core/paragraph' );
                break;
            case 'my_cpt_2':
                return array( 'core/paragraph', 'core/heading' );
                break;
            default:
                return true;
        }
    
        return $allowed_block_types;
    
    }
    add_filter( 'allowed_block_types', 'myplugin_allowed_block_types', 10, 2 );

  2. Skip to note 6 content

    This allows only paragraph block for post.

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

You must log in before being able to contribute a note or feedback.