apply_filters( 'allowed_block_types', bool|array $allowed_block_types, WP_Post $post )

Filters the allowed block types for the editor, defaulting to true (all block types supported).


Parameters Parameters

$allowed_block_types

(bool|array) Array of block type slugs, or boolean to enable/disable all.

$post

(WP_Post) The post resource data.


Top ↑

Source Source

File: wp-admin/edit-form-blocks.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Nilambar Sharma

    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 );
    
  2. Skip to note 2 content
    Contributed by Katrina Yancey

    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 );
    
    
    

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