allowed_block_types_all
Allows removing blocks (leaving only those permitted) for all types of block editors.
When using this filter:
- Blocks registered through JavaScript but not included here as permitted are also disabled (removed).
- Any block patterns composed of disabled blocks are also disabled (removed).
Use WP_Block_Type_Registry::get_all_registered() to obtain a list of all available blocks.
See also: How to disable specific blocks in WordPress.
Usage
add_filter( 'allowed_block_types_all', 'wp_kama_allowed_block_types_all_filter', 10, 2 );
/**
* Function for `allowed_block_types_all` filter-hook.
*
* @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*
* @return bool|string[]
*/
function wp_kama_allowed_block_types_all_filter( $allowed_block_types, $block_editor_context ){
// filter...
return $allowed_block_types;
}
- $allowed_block_types(true|false|string[])
An array of block names, or a boolean value:
trueenables all blocks.falsedisables all blocks.
Default: true (all blocks are allowed)
- $block_editor_context(WP_Block_Editor_Context)
The current block editor context.
$block_editor_context->namecan be:core/edit-post– Editorcore/edit-site– Site Editorcore/widgets– Widgets Editorcore/customize-widgets– Widgets Editor in the Customizer
Examples
#1 Disable blocks with PHP (allowlist)
This simple example allows users to use only the Heading, List, Image, and Paragraph blocks; all other blocks are disabled.
add_filter( 'allowed_block_types_all', 'example_allowed_block_types', 10, 2 );
function example_allowed_block_types( $allowed_block_types, $block_editor_context ) {
$allowed_block_types = array(
'core/heading', // Heading block
'core/image', // Image block
'core/list', // List block
'core/paragraph', // Paragraph block
);
return $allowed_block_types;
}
Result:
#2 Contents of the $block_editor_context parameter
The following example shows the contents of the second $block_editor_context parameter:
add_filter( 'allowed_block_types_all', function( $allowed_block_types, $block_editor_context ) {
print_r( $block_editor_context );
return $allowed_block_types;
}, 10, 2 );
Output:
WP_Block_Editor_Context Object ( [name] => core/edit-post [post] => WP_Post Object ( [ID] => 257 [post_author] => 1 [post_date] => 2021-08-23 19:30:21 [post_date_gmt] => 2021-08-23 16:30:21 [post_content] => Some post content [post_title] => Ketchup [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => ketchup [to_ping] => [pinged] => [post_modified] => 2024-04-12 04:04:15 [post_modified_gmt] => 2024-04-12 01:04:15 [post_content_filtered] => [post_parent] => 0 [guid] => https://example.loc/?page_id=257 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 [filter] => raw ) )
Example of using the parameter:
add_filter( 'allowed_block_types_all', 'example_allowed_block_types_when_editing_posts', 10, 2 );
function example_allowed_block_types_when_editing_posts( $allowed_block_types, $block_editor_context ) {
// Only apply in the Editor when editing posts.
if (
'core/edit-post' === $block_editor_context->name &&
isset( $block_editor_context->post ) &&
'post' === $block_editor_context->post->post_type
) {
$allowed_block_types = array(
'core/heading', // Heading block
'core/image', // Image block
'core/list', // List block
'core/paragraph', // Paragraph block
);
return $allowed_block_types;
}
// Allow all blocks in the Site Editor or when editing other post types.
return true;
}
#3 Exclude unwanted blocks
The hook accepts a list of allowed blocks. If you have hundreds of blocks, however, it is more convenient to specify only those that need to be excluded.
To do this, put unwanted blocks in an array, programmatically retrieve the complete list of registered blocks, and remove the specified blocks from it.
See the complete list of core blocks here.
add_filter( 'allowed_block_types_all', 'wpkama_disable_gutenberg_blocks', 30, 2 );
function wpkama_disable_gutenberg_blocks( $allowed_blocks, $block_editor_context ) {
$disable_blocks = [
//'core/archives',
//'core/audio',
//'core/avatar',
//'core/block',
//'core/button',
//'core/buttons',
'core/calendar',
//'core/categories',
//'core/code',
//'core/column',
//'core/columns',
'core/comment-author-name',
'core/comment-content',
'core/comment-date',
'core/comment-edit-link',
'core/comment-reply-link',
'core/comment-template',
'core/comments',
'core/comments-pagination',
'core/comments-pagination-next',
'core/comments-pagination-numbers',
'core/comments-pagination-previous',
'core/comments-title',
//'core/cover',
//'core/details',
'core/embed',
//'core/file',
//'core/footnotes',
//'core/freeform',
//'core/gallery',
//'core/group',
//'core/heading',
//'core/home-link',
//'core/html',
//'core/image',
//'core/latest-comments',
//'core/latest-posts',
//'core/legacy-widget',
//'core/list',
//'core/list-item',
//'core/loginout',
//'core/media-text',
//'core/missing',
//'core/more',
//'core/navigation',
//'core/navigation-link',
//'core/navigation-submenu',
//'core/nextpage',
//'core/page-list',
//'core/page-list-item',
//'core/paragraph',
//'core/pattern',
//'core/post-author',
'core/post-author-biography',
//'core/post-author-name',
'core/post-comments',
'core/post-comments-form',
//'core/post-content',
//'core/post-date',
//'core/post-excerpt',
//'core/post-featured-image',
//'core/post-navigation-link',
//'core/post-template',
//'core/post-terms',
//'core/post-title',
//'core/preformatted',
//'core/pullquote',
//'core/query',
//'core/query-no-results',
//'core/query-pagination',
//'core/query-pagination-next',
//'core/query-pagination-numbers',
//'core/query-pagination-previous',
//'core/query-title',
//'core/quote',
//'core/read-more',
'core/rss',
//'core/search',
//'core/separator',
//'core/shortcode',
//'core/site-logo',
//'core/site-tagline',
//'core/site-title',
'core/social-link',
'core/social-links',
//'core/spacer',
//'core/table',
//'core/tag-cloud',
//'core/template-part',
//'core/term-description',
//'core/text-columns',
//'core/verse',
//'core/video',
//'core/widget-group',
];
// Get all registered blocks if $allowed_block_types is not already set.
if( ! is_array( $allowed_blocks ) || empty( $allowed_blocks ) ){
$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
$allowed_blocks = array_keys( $registered_blocks );
}
foreach( $allowed_blocks as $index => $block_id ){
if( in_array( $block_id, $disable_blocks, true ) ){
unset( $allowed_blocks[ $index ] );
}
}
return array_values( $allowed_blocks ); // note: array list required
}
The code keeps the complete list of blocks that were available by default as comments, so you can copy it and uncomment the blocks you do not need.
Changelog
| Since 5.8.0 | Introduced. |
Where the hook is called
$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
