Disable All WordPress.com and Core Block Patterns
While typically not the best practice, you may want to disable all WordPress.com and core patterns on a site so that your clients can only use the patterns you’ve created specifically for them.
To disable all non-user-created patterns, copy and paste the following code into your functions.php
file:
if ( ! function_exists( 'your_theme_restrict_block_editor_patterns' ) ) {
/**
* Restricts block editor patterns in the editor by removing support for all patterns from:
* - Dotcom and plugins like Jetpack
* - Dotorg pattern directory except for theme patterns
*/
function your_theme_restrict_block_editor_patterns( $dispatch_result, $request, $route ) {
if ( strpos( $route, '/wp/v2/block-patterns/patterns' ) === 0 ) {
$patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
if ( ! empty( $patterns ) ) {
// Remove theme support for all patterns from Dotcom, and plugins. See https://developer.wordpress.org/themes/features/block-patterns/#unregistering-block-patterns
foreach ( $patterns as $pattern ) {
unregister_block_pattern( $pattern['name'] );
}
// Remove theme support for core patterns from the Dotorg pattern directory. See https://developer.wordpress.org/themes/features/block-patterns/#removing-core-patterns
remove_theme_support( 'core-block-patterns' );
}
}
return $dispatch_result;
}
}
// Remove and unregister patterns from core, Dotcom, and plugins. See https://github.com/Automattic/jetpack/blob/d032fbb807e9cd69891e4fcbc0904a05508a1c67/projects/packages/jetpack-mu-wpcom/src/features/block-patterns/block-patterns.php#L107
add_filter( 'rest_dispatch_request', 'your_theme_restrict_block_editor_patterns', 12, 3 );
// Disable the remote patterns coming from the Dotorg pattern directory. See https://developer.wordpress.org/themes/features/block-patterns/#disabling-remote-patterns
add_filter( 'should_load_remote_block_patterns', '__return_false' );
Last updated: April 09, 2024