How to disable the block directory search from the WordPress editor

One of the most recent additions to the block editor – known as Gutenberg – is the search in the block directory for extra blocks(through plugins).

And wow, yes, it’s convenient, because if you’re looking for a block to insert in your content and, let’s put in situation, you don’t know what blocks you have available, and it turns out that you don’t have anything similar to what you want to insert, well, the editor makes an instant search in the blocks directory and offers you the available ones to install and activate in your content, which is good and useful.

But, like everything, it has its drawbacks:

  • It slows down the load of the WordPress editor.
  • Allows to add blocks to any user with permission to install and activate plugins (by default only admins but you can not discriminate more in detail).
  • Encourages indiscriminate installation of plugins.

So, in my opinion, something should be done to control, or even disable, the block installer from the directory in the WordPress editor, don’t you think?

Completely disable the installation of plugins, blocks or whatever.

If we get radical, with this constant added to the wp-config.php file we will completely disable the installation of plugins, themes, file editing, we will have a safe environment in terms of file management from the administration:

Careful with this as you may lose access to a lot of customization options that you may want to use in the future.

define('DISALLOW_FILE_MODS',true);
Code language: JavaScript (javascript)

Completely disable the search and install directory blocks in the WordPress editor

Now, although the above constant works, we still haven’t solved the optimization problem, because the editor will still make calls via the REST API to fetch blocks from the editor.

So you don’t need to be that extreme (although you can), or you can add to the above trick this code, which you will need to add to your customizations plugin:

/* Disable block directory search on editor */ add_action( 'admin_init', function() { remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' ); //remove_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets_block_directory' ); /* Activate this too if you have the Gutenberg plugin active */ } );
Code language: JavaScript (javascript)

With this code the editor will no longer even try to search for blocks in the directory, it will only search among the active blocks.

Disable, depending on the environment, the search and installation of the block directory in the WordPress editor.

A very smart way to disable the search in the blocks directory from the editor would be to do it according to the development environment defined in the wp-config.php file.

So, for example, if you want to have it active while you are developing, to install the blocks you need, but inactive when you move the site to production, you would have to add this custom code:

/* Disable block directory search on editor when your site is live *( add_action( 'admin_init', function() { if ( wp_get_environment_type() != 'production' ) { remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' ); //remove_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets_block_directory' ); /* Activate this too if you have the Gutenberg plugin active */ } } );
Code language: JavaScript (javascript)

Disable the search and install directory blocks in the WordPress editor on a per user basis

To finalize, and to have full control, a good idea would be to specify which user(s) should have access to the search engine integrated in the block directory, excluding all others, regardless of their user profile.

The code would look like this:

/* Disable block directory search on editor depending on user */ add_action( 'admin_init', function() { if ( ! in_array( wp_get_current_user()->user_login, array( '{{username}}' ) ) ) { remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' ); //remove_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets_block_directory' ); /* Activate this too if you have the Gutenberg plugin active */ } } );
Code language: PHP (php)

In this code, you must change {{username}} to the slug of the user that you allow to search and install blocks from the editor. Of course, this user must be an administrator, and not have limited install_plugins and activate_plugins capabilities.


And already, what you were looking for you get it, that the WordPress editor no longer searches the blocks directory to install more blocks.

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top Skip to content