Home / Block and Plugin Tutorials / Remove Core and Third-party Block Patterns

Remove Core and Third-party Block Patterns

Block Patterns are a very useful feature when developing a site with blocks. However, there is sometimes too much choice and you may want to limit the block patterns that can be inserted in the content.

With a recent project we created several in-house block patterns, but didn’t want to confuse the content team when it came to finding and inserting them. We decided to disable core patterns, prevent remote pattern lookup, and to deregister any third-party patterns as well.

Here’s how we did it.

Creating the “Plugin”

In this example, I’m just going to create a plugin file and use it as an mu-plugin because I wouldn’t want anybody to be able to deactivate this functionality.

If you are not familiar with mu-plugins, they are typically single file PHP files in an mu-plugins folder inside wp-content.

.
└── wp-content/
    ├── plugins
    ├── themes
    └── mu-plugins/
        └── dlx-remove-patterns.phpCode language: AsciiDoc (asciidoc)

Let’s give our plugin some basic plugin headers so that they show up in our admin plugins screen.

<?php
/**
 * Plugin Name:       DLX Remove Block Patterns
 * Plugin URI:        https://dlxplugins.com/plugins/alertsdlx/
 * Description:       Remove Block Patterns and third-party patterns.
 * Version:           1.1.0
 * Requires at least: 6.0
 * Requires PHP:      7.2
 * Author:            DLX Plugins
 * Author URI:        https://dlxplugins.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 *
 */Code language: PHP (php)

Here’s how you’d see it in the admin.

Must-use Plugins Screen
Must-use Plugins Screen

Now that we have the plugin going let’s disable remote pattern lookup.

Disabling Remote Pattern Lookup

Block Pattern Screen with Remote Lookup
Block Pattern Screen with Remote Lookup

To prevent WordPress from loading in patterns remotely, add this line to the plugin:

/**
 * This prevents loading of remote patterns.
 */
add_filter( 'should_load_remote_block_patterns', '__return_false' );Code language: PHP (php)

Removing Core Patterns

Now that remote patterns are disabled, let’s remove all core patterns. I’m going to hook into the init action and remove core pattern support.

/**
 * Remove Core patterns.
 */
function dlx_remove_core_block_patterns() {
	remove_theme_support( 'core-block-patterns' );
}
add_action( 'init', 'dlx_remove_core_block_patterns', 100 ); // Note the priority.Code language: PHP (php)

Removing theme support for the core block patterns should happen during the init action, and I set a priority of 100 so that the theme and plugins would be loaded in by then and that we should have no conflicts removing the core pattern support.

Removing Registered Patterns

Now we need to remove/disable all of the registered patterns. I’ll just be adding to the function above to remove these.

/**
 * Remove Core patterns and third-party patterns.
 */
function dlx_remove_core_block_patterns() {
	$registered_patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
	if ( $registered_patterns ) {
		foreach ( $registered_patterns as $pattern_properties ) {
			unregister_block_pattern( $pattern_properties['name'] );
		}
	}
	remove_theme_support( 'core-block-patterns' );
}
add_action( 'init', 'dlx_remove_core_block_patterns', 100 ); // Note the priority.Code language: PHP (php)

We get a list of all registered patterns and unregister them (again, at priority 100).

If all is well, you shouldn’t have a Block Patterns tab anymore.

No Block Patterns Present in the Block Inserter
No Block Patterns Present in the Block Inserter

Registering Your Custom Patterns

Finally, you’ll want to register any of your custom patterns. You’ll need to set the priority higher than 100, so in this case we’ll use 101.

/**
 * Register any of our custom block patterns.
 */
function dlx_register_custom_block_patterns() {
	// Register your custom block patterns here.
}
add_action( 'init', 'dlx_register_custom_block_patterns', 101 );
Code language: PHP (php)

The Code

You can find the entire plugin file by following this Gist: Remove Core and Third-party Patterns.

<?php
/**
 * Plugin Name:       DLX Remove Block Patterns
 * Plugin URI:        https://dlxplugins.com/plugins/alertsdlx/
 * Description:       Remove Block Patterns and third-party patterns.
 * Version:           1.1.0
 * Requires at least: 6.0
 * Requires PHP:      7.2
 * Author:            DLX Plugins
 * Author URI:        https://dlxplugins.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 *
 */

/**
 * This prevents loading of remote patterns.
 */
add_filter( 'should_load_remote_block_patterns', '__return_false' );

/**
 * Remove Core patterns and third-party patterns.
 */
function dlx_remove_core_block_patterns() {
	$registered_patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
	if ( $registered_patterns ) {
		foreach ( $registered_patterns as $pattern_properties ) {
			unregister_block_pattern( $pattern_properties['name'] );
		}
	}
	remove_theme_support( 'core-block-patterns' );
}
add_action( 'init', 'dlx_remove_core_block_patterns', 100 ); // Note the priority.

/**
 * Register any of our custom block patterns.
 */
function dlx_register_custom_block_patterns() {
	// Register your custom block patterns here.
}
add_action( 'init', 'dlx_register_custom_block_patterns', 101 );

Code language: PHP (php)

Conclusion

I do indeed see a use-case for block patterns, but in this particular case, we didn’t want the client inserting block patterns that weren’t pre-vetted to maintain design consistency.

Like this tutorial? There's more like it. Subscribe today!

Name(Required)

Ronald Huereca
By: Ronald Huereca
Published On: on January 25, 2023

Ronald Huereca founded DLX Plugins in 2022 with the goal of providing deluxe plugins available for download. Find out more about DLX Plugins, check out some tutorials, and check out our plugins.

Leave Your Valuable Feedback

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

Shopping Cart
  • Your cart is empty.
Scroll to Top