Skip to content

Instantly share code, notes, and snippets.

@lunule
Last active March 20, 2024 01:48
Show Gist options
  • Save lunule/0f8c61c2fa1d4ee54f24d46b3f1f81e1 to your computer and use it in GitHub Desktop.
Save lunule/0f8c61c2fa1d4ee54f24d46b3f1f81e1 to your computer and use it in GitHub Desktop.
[WP - Full Site Editing - Remove Preregistered Patterns] #unregister #deregister #remove #prevent #wp #fse #patterns https://www.wpexplorer.com/how-to-disable-wordpress-gutenberg-block-patterns/
<?php
/**
* @here https://www.wpexplorer.com/how-to-disable-wordpress-gutenberg-block-patterns/
*/
//Remove core patterns:
//
//The following code will remove the default core patterns that are installed in WordPress natively.
add_action( 'after_setup_theme', function() {
remove_theme_support( 'core-block-patterns' );
} );
//Remove remote patterns:
//The following code will disable the official patterns from wordpress.org/patterns which as the
//name implies are loaded remotely as opposed to being part of the the WordPress files installed
//on your server.
add_filter( 'should_load_remote_block_patterns', '__return_false' );
//Remove theme patterns:
//From what I can see removing theme patterns is a bit more complex as there doesn’t seem to be
//any filter that we can use to quickly remove them. So what we need to do is grab all the
//patterns loop through them and remove any that have the same name as your theme.
add_action( 'init', function() {
if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) {
return;
}
$theme_slug = 'pineapple-wpex';
$patterns = (array) WP_Block_Patterns_Registry::get_instance()->get_all_registered();
foreach ( $patterns as $pattern ) {
if ( isset( $pattern['name'] ) && str_starts_with( $pattern['name'], $theme_slug ) ) {
unregister_block_pattern( $pattern['name'] );
}
}
} );
//Remove specific patterns:
//
//It’s also possible to remove specific patterns only. Below is an example showing how to
//remove the “core/social-links-shared-background-color” pattern which is the social links
//pattern located in the “Call to Action” category.
add_action( 'init', function() {
if ( ! function_exists( 'unregister_block_pattern' ) ) {
return;
}
unregister_block_pattern( 'core/social-links-shared-background-color' );
} );
//If you aren’t sure how to locate the name of a specific block pattern to remove it you
//can check out the previous snippet. It shows how to get a list of registered block
//patterns which you could pass through var_dump() or error_log().
/**
* Important: If you try and remove a block pattern that doesn’t exist WordPress will
* throw a PHP error!
* ==================================================================================
* ==================================================================================
* ==================================================================================
*/
/**
* FYKI - the below implementation might need to be updated later, based on
* how Automattic handles these forced patters.
*
* e.g. they might become a core feature - in which case the Guenberg
* plugin status check applied below needs to be removed.
*/
/* ================================================================================== */
/* ================================================================================== */
/* ================================================================================== */
/**
* Remove support for remote patterns enforeced by the Gutenberg plugin - this
* is currently the only way to prevent the load of the following remote query
* patterns:
*
* 'core/fullwidth-posts-with-uppercase-titles'
* 'core/fullwidth-posts-titles-with-dates'
*
* @since FSE Pilot 1.0
*/
if ( is_plugin_active( 'gutenberg/gutenberg.php' ) )
add_filter( 'should_load_remote_block_patterns', '__return_false' );
/**
* ==================================================================================
* FYKI: disabling this filter means the theme user won't be able to use remote
* patterns, AT ALL.
* Meaning, it's more recommended for theme authors to take the time and
* START THE QUERY PATTERN CUSTOM STYLING WITH THESE TWO PATTERNS.
*
* Why STARTING WITH THEM?
* Because these remote patterns don't have any identifier - practically
* it's impossible to create styles only applied on these patterns.
* By starting the query pattern design process with these two, it will
* be an easier and less stressing process to overwrite these "default"
* pattern styles with the ones specifically created for the theme's own
* query patterns.
* ==================================================================================
*/
/**
* Unregister query patterns enforeced by the Gutenberg plugin
*
* @since FSE Pilot 1.0
*
* @return void
*/
function fse_pilot_unregister_core_patterns() {
if ( ! function_exists( 'unregister_block_pattern' ) )
return;
if ( !is_plugin_active( 'gutenberg/gutenberg.php' ) )
return;
unregister_block_pattern( 'core/query-standard-posts' );
unregister_block_pattern( 'core/query-medium-posts' );
unregister_block_pattern( 'core/query-small-posts' );
unregister_block_pattern( 'core/query-grid-posts' );
unregister_block_pattern( 'core/query-large-title-posts' );
unregister_block_pattern( 'core/query-offset-posts' );
};
add_action( 'init', 'fse_pilot_unregister_core_patterns' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment