Skip to content

Instantly share code, notes, and snippets.

@khleomix
Last active February 16, 2022 20:30
Show Gist options
  • Save khleomix/32cc8e6db9b7763a4d9b956760e1b390 to your computer and use it in GitHub Desktop.
Save khleomix/32cc8e6db9b7763a4d9b956760e1b390 to your computer and use it in GitHub Desktop.
Disable/Allow core Gutenberg Blocks
<?php
/**
* Modify the available blocks for the Gutenberg editor
*
*/
namespace Theme\Theme\Blocks;
use \WP_Block_Type_Registry;
/**
* Filter the allowed blocks for the block editor
*
* @param array $allowed_block_types Current list of allowed blocks.
* @return array Filtered list of allowed blocks
*/
function allowed_blocks( $allowed_block_types ) {
$core_blocks_to_keep = array(
'core/freeform',
'core/paragraph',
'core/heading',
'core/list',
'core/image',
'core/table',
'core/separator',
'core/gallery',
'core/embed',
);
// Find the currently registered blocks
// The value being filtered can either be a boolean or an array.
$currently_allowed_blocks = is_array( $allowed_block_types ) ? $allowed_block_types : array();
// If we have a true boolean type, then all blocks are permitted.
if ( true === $allowed_block_types ) {
$registry = WP_Block_Type_Registry::get_instance();
// See: https://developer.wordpress.org/reference/classes/wp_block_type_registry/get_all_registered/
$currently_allowed_blocks = array_keys( $registry->get_all_registered() );
}
// Include all ACF blocks
$custom_allowed_blocks = array_values(
array_filter(
$currently_allowed_blocks,
function( $block_type ) {
return strpos( $block_type, 'acf/' ) === 0;
}
)
);
return array_merge( $core_blocks_to_keep, $custom_allowed_blocks );
}
// Filter changed at WordPress 5.8.
// See https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#allowed_block_types_all
$block_filter_name = class_exists( 'WP_Block_Editor_Context' ) ? 'allowed_block_types_all' : 'allowed_block_types';
add_filter( $block_filter_name, __NAMESPACE__ . '\allowed_blocks', 99, 1 );
/**
* Remove 'core-block-patterns' from theme support to avoid cluttering the editor.
*/
function disable_core_block_patterns() {
remove_theme_support( 'core-block-patterns' );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\disable_core_block_patterns', 10, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment