Skip to content

Disable WordPress blocks for all users

Blog  ✺  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.

[
  'core/image', 
  'core/headline', 
  'core/buttons', 
  'core/button', 
  // ... 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.


Update January 31, 2024

There is now a more in-depth guide available that allows you to disable and enable certain blocks for the individual block editor instances like the site editor:

How to disable specific blocks in WordPress
Have you ever wanted to disable specific blocks in WordPress? Whether to simplify the editing experience, maintain consistency across a site, or any other reason, this article will guide you throug…

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.

Comments

You might like

Add Dimensions to Image and Video Uploads in Laravel

Laravel

Calm: Peaceful Pictures

Opinion  ✺  Photography