Skip to content

Disable WordPress blocks for all users

Writing  ✺  WordPress  ✺  Code Snippet

The list of blocks in the WordPress editor is growing with every new release. For a client project that I'm working on, I needed a way to disable a few core blocks for certain post types.

After asking on X, my buddy Mark Howells-Mead pointed me in the right direction with his reply. He suggested using the allowed_block_types filter. Because this filter is deprecated, I opted for the more recent replacement allowed_block_types_all. Place the code in your theme functions.php file or use a plugin like Code Snippets.

The code snippet

This code works with PHP 8 or later. Because we are using the null safe operator.

Which blocks to disable?

In the array we are defining blocks that we would like to disable. You can find a list of all core blocks in the handbook.

  // ... add more blocks

All the blocks in this list will be disabled for all users. In the post types you defined before.

What post types to disable the blocks on?

As the block editor is now used on the Widget, Template and other screens in recent WordPress versions, we use the second argument of the filter to see what context we are on and only disable the blocks for certain post types:

if ( in_array( $editor_context->post?->post_type, ['post', 'page'] ) ) {

  $blocks = array_diff( 
    array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ),
    // ... rest of the code

Only if we are in the context of a post type, in our case ['post', 'page'] will we load all registered blocks in WordPress, otherwise we will return the initial list of blocks.

Did you like it?

Let me know on X if you liked this code snippet and how you ended up using it for your project.


You might like

Code Snippet: Remove Jetpack Related Posts from Woo Testimonials

Installing the 108 most popular WordPress plugins at once