allowed_block_types
Deprecated since 5.8.0. It is no longer supported and may be removed in future releases. See allowed_block_types_all.
Allows you to retain only the required blocks in the Block Editor (Gutenberg).
The filter's default value is true, which means that all available blocks are displayed.
The hook is deprecated as of WP 5.8; use allowed_block_types_all. It fires on the post editing page when the block editor is enabled.
Usage
add_filter( 'allowed_block_types', 'wp_kama_allowed_block_types_filter', 10, 3 );
/**
* Function for `allowed_block_types` filter-hook.
*
* @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
* Default true (all registered block types supported)
* @param WP_Post $post The post resource data.
* @param $string
*
* @return bool|string[]
*/
function wp_kama_allowed_block_types_filter( $allowed_block_types, $post, $string ){
// filter...
return $allowed_block_types;
}
- $allowed_block_types(true/false/array)
- Array of block slugs to retain (display) in the editor. Alternatively,
trueorfalseto enable or disable all blocks. See the slug list below. - $post(WP_Post)
- Current post object.
Examples
#1 Disable all blocks except those specified
add_filter( 'allowed_block_types', 'allow_my_block_types', 10, 2 );
function disallow_noneed_block_types( $allowed_blocks, $post ){
return [
'core/heading',
'core/paragraph',
'core/image',
'core/list'
];
}
#2 Possible slugs for the $allowed_block_types parameter
Common blocks
core/paragraph core/image core/heading core/gallery core/list core/quote core/audio core/cover core/file core/video
Formatting
core/table core/verse core/code core/freeform core/html core/preformatted core/pullquote
Layout Elements
core/button core/columns core/text-columns core/media-text core/more core/nextpage core/separator core/spacer
Widgets
core/shortcode core/archives core/categories core/latest-comments core/latest-posts
Embeds
core/embed core-embed/twitter core-embed/youtube core-embed/facebook core-embed/instagram core-embed/wordpress core-embed/soundcloud core-embed/spotify core-embed/flickr core-embed/vimeo core-embed/animoto core-embed/cloudup core-embed/collegehumor core-embed/dailymotion core-embed/funnyordie core-embed/hulu core-embed/imgur core-embed/issuu core-embed/kickstarter core-embed/meetup-com core-embed/mixcloud core-embed/photobucket core-embed/polldaddy core-embed/reddit core-embed/reverbnation core-embed/screencast core-embed/scribd core-embed/slideshare core-embed/smugmug core-embed/speaker core-embed/ted core-embed/tumblr core-embed/videopress core-embed/wordpress-tv
#3 Enable blocks for a post type
This example shows how to enable the shortcode block only for the post and page post types:
add_filter( 'allowed_block_types', 'allow_my_block_types', 10, 2 );
function allow_my_block_types( $allowed_blocks, $post ){
$allowed_blocks = [
'core/image',
'core/paragraph',
'core/heading',
'core/list'
];
if( in_array( $post->post_type, ['post','page'] ) ){
$allowed_blocks[] = 'core/shortcode';
}
return $allowed_blocks;
}Changelog
| Since 5.0.0 | Introduced. |
| Deprecated since 5.8.0 | Use the {@see 'allowed_block_types_all'} filter instead. |
Where the hook is called
allowed_block_types
wp-includes/block-editor.php 142
$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
