When it comes to performance improvements, I guess one of the last things to consider are block patterns. But here we are. As I noticed that I have multiple duplicate SQL queries regarding WooCommerce’s block patterns, I checked how to disable them.

Actually, I monitored my local environment via Query Monitor and noticed 14 duplicate SQL queries. All of them were regarding different block pattern registrations and all came from WooCommerce. So I wanted to disable them.

Yes, you can easily disable block patterns by unregister them after they have been registered. However, this doesn’t stop them from being registered in the first place and thus producing duplicate SQL queries for me. So I looked for a way to disable them completely, since I don’t use them anyway.

Unfortunately, there is no setting (yet). I found an open issue about disabling WooCommerce block patterns, but it’s still open and not implemented so far. Then, I found an article from OnPoint Plugins about Disabling WooCommerce Blocks, where I could found the first part of the solution by removing the action added by WooCommerce. But while this post removes the WooCommerce blocks itself, it doesn’t touch the block patterns. So I checked the source code to see, which WooCommerce package is registering the patterns.

There are multiple areas where block patterns are registered. Two of them can be disabled this way (see lines 10 – 13), while others can be removed directly from the wp_loaded hook (lines 18 – 37).

The complete code looks like this and can be used as an MU plugin in /wp-content/mu-plugins/.

<?php
use Automattic\WooCommerce\Blocks\BlockPatterns;
use Automattic\WooCommerce\Blocks\BlockTypesController;
use Automattic\WooCommerce\Blocks\BlockTypes\Cart;
use Automattic\WooCommerce\Blocks\Package;

/**
 * Remove default block patterns.
 */
\add_action( 'woocommerce_blocks_loaded', static function() {
	\remove_action( 'init', [ Package::container()->get( BlockPatterns::class ), 'register_block_patterns' ] );
	\remove_action( 'init', [ Package::container()->get( BlockPatterns::class ), 'register_ptk_patterns' ] );
} );

/**
 * Remove additional block patterns.
 */
\add_action( 'wp_loaded', static function() {
	global $wp_filter;
	
	foreach ( $wp_filter['wp_loaded']->callbacks[10] as $filter ) {
		if ( ! is_callable( $filter['function'] ) ) {
			continue;
		}
		
		if ( ! is_array( $filter['function'] ) ) {
			continue;
		}
		
		if (
			( $filter['function'][0] instanceof Cart && $filter['function'][1] === 'register_patterns' )
			|| ( $filter['function'][0] instanceof BlockTypesController && $filter['function'][1] === 'register_block_patterns' )
		) {
			\remove_action( 'wp_loaded', $filter['function'] );
		}
	}
}, \PHP_INT_MIN );
Code language: PHP (php)

Likes

Reposts

Leave a Reply

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

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn more about webmentions)