Closed liviopv closed 1 week ago
Support References
This comment is automatically generated. Please do not edit it.
They can unregister patterns via their theme's functions.php. See how the Assembler theme does remove core and Jetpack patterns here.
In order to allow developers to unregister the Dotcom pattern library, we can implement and provide a hook for that.
The following snippet has to be pasted in the functions.php file of a theme to disable block patterns from core, Dotcom, and plugins.
I added details in the code comments so developers can understand which code to tweak to enable some patterns.
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
*
* @return mixed
*/
function your_theme_restrict_block_editor_patterns( $dispatch_result, $request, $route ) {
$is_patterns_request = preg_match( '/^\/wp\/v2\/block\-patterns\/patterns$/', $route );
if ( $is_patterns_request ) {
// For the request route /wp/v2/block-patterns/patterns
$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;
}
endif;
// 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' );
The result is that the "Patterns" tab is not visible because no patterns are registered.
📌 REPRODUCTION RESULTS
Feature request kept and already assigned to the relevant team.
We can find the support doc to Disable all patterns at https://developer.wordpress.com/docs/developer-tools/block-patterns/disable-all-patterns
Also a note in the FAQs section of Using Patterns on WordPress.com at https://developer.wordpress.com/docs/developer-tools/block-patterns/#6-frequently-asked-questions 🎉
@miksansegundo I tried the snippet on my test site with the Ollie theme (third-party) to see if it removes only our default patterns, but it removed everything including the patterns from the theme. Is this expected?
Context: I had a user today who uses the Ollie theme, and they requested removing our default patterns in https://SITE_URL/wp-admin/site-editor.php?postType=wp_block/.
@hacchism that was not expected but something has changed because that's now the case.
To remove only the patterns from the Dotcom library you can use the following snippet:
if ( ! function_exists( 'your_theme_restrict_block_editor_patterns_from_dotcom_library' ) ) {
/**
* Remove Dotcom library patterns from the editor inserters.
*/
function your_theme_restrict_block_editor_patterns_from_dotcom_library() {
return 'disable-dotcom-patterns-source';
}
}
// Remove patterns from the Dotcom library by disabling the patterns source site.
add_filter( 'a8c_override_patterns_source_site', 'your_theme_restrict_block_editor_patterns_from_dotcom_library' );
Another option with the same result that allows more control to filter patterns is the following:
if ( ! function_exists( 'your_theme_restrict_block_editor_patterns_from_dotcom_library_only' ) ) {
/**
* Remove Dotcom library patterns from the editor inserters.
*/
function your_theme_restrict_block_editor_patterns_from_dotcom_library_only( $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 ) ) {
foreach ( $patterns as $pattern ) {
// Unregister patterns with namespace `a8c`.
// Other libraries and plugins use different namespaces like `woo/` or `your-theme/`
if ( substr( $pattern['name'], 0, 4 ) === 'a8c/' ) {
unregister_block_pattern( $pattern['name'] );
}
}
}
}
return $dispatch_result;
}
}
// Unregister patterns from the Dotcom library by filtering using the A8C namespace.
add_filter( 'rest_dispatch_request', 'your_theme_restrict_block_editor_patterns_from_dotcom_library_only', 12, 3 );
Remember to use only one snippet at a time and place it at the end of the functions.php file of your theme.
@miksansegundo Thanks for these! Working on getting that doc updated.
Are the code blocks above doing the same thing, or is the bottom one restricting all block patterns from core and dotcom?
@alexapeduzzi Both code blocks in that comment are doing the same thing: Remove from the editor inserters the patterns from the Dotcom library only.
Great! Thanks, @miksansegundo. I've updated the doc: https://developer.wordpress.com/docs/developer-tools/block-patterns/disable-all-patterns
Closing this issue with the addition of the section "Disable Only WordPress.com Block Patterns" in the docs at https://developer.wordpress.com/docs/developer-tools/block-patterns/disable-all-patterns/#1-disable-only-wordpress-com-block-patterns
Thanks for the request!
What
Currently, WordPress.com loads a bunch of Block Patterns and there's no easy way to disable them, if the user don't want to see them, or wants to remove them from the site for whatever reason
Why
While this is an excellent feature in most cases, it also creates a lot of bloat in the pattern inserter (which users might want to avoid), and it also prevents developers from restricting clients from using certain patterns and messing up the site (ref 7813027-zd-a8c).
How
Ideally, there should be a toggle under Settings > Writing where users can turn off WordPress.com Patter Collection, or the Patterns should be tied to something like ETK, where deactivating the plugin gets rid of the patterns as well.
More feasibly, we can also create a support doc on https://developer.wordpress.com/docs/ with a custom PHP snippet that devs can add to their site.