The block directory in WordPress 5.5: install blocks from the editor, or turn it off
WordPress 5.5 added the block directory to the editor. When an editor searches the block inserter for something that no installed block provides, WordPress also searches the single-block plugins on WordPress.org and offers them with an “Add block” button. One click installs and activates the plugin and inserts the block. For editors this is convenient; for agencies that keep client sites under control, it is a way for new plugins to arrive without anybody reviewing them.
Problem: On a managed client site, plugins should be installed only after review, but the block inserter offers to install new block plugins directly from the editor.
Solution: Remove the action that loads the block directory assets, wp_enqueue_editor_block_directory_assets, from enqueue_block_editor_assets. On a network, do it in an mu-plugin. Users without the plugin capabilities do not see the directory anyway.
Who sees it
The directory appears only for users who may install and activate plugins (install_plugins and activate_plugins), so in a default setup only administrators. Editors and authors never see it. It also does not appear when plugin installation is disabled with DISALLOW_FILE_MODS.
What gets installed is a normal plugin: it shows up in Plugins, can be deactivated and deleted there, and gets updates like any other plugin. The directory contains only plugins that provide exactly one block and follow the block directory guidelines.
Turning it off
<?php
/**
* Plugin Name: No block directory
*/
add_action( 'plugins_loaded', function () {
remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' );
} );
Without the assets, the inserter shows only installed blocks. As an mu-plugin (wp-content/mu-plugins/no-block-directory.php) it covers every site of a network and cannot be deactivated from the admin.
If administrators should not install anything from the admin at all, the stronger switch is in wp-config.php:
define( 'DISALLOW_FILE_MODS', true ); // no plugin or theme installs and updates from the admin, no block directory
That also stops updates from the admin, so it fits only sites that are updated through deployment.
Checking which blocks are available
<?php
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'core/paragraph' ) ) {
// the block exists on this site
}
foreach ( array_keys( $registry->get_all_registered() ) as $name ) {
error_log( $name ); // core/paragraph, core/heading, ...
}
This lists blocks registered in PHP. Blocks registered only in JavaScript are known only in the editor; there, wp.blocks.getBlockTypes() returns all of them.
NOTE: Turning the directory off does not limit what administrators can do; they can still install any plugin from the Plugins screen. It only removes the shortcut that makes installing a plugin feel like inserting a block, which is the part that surprises clients.