Home / Disable Specific Blocks
Duplicate Snippet

Embed Snippet on Your Site

Disable Specific Blocks

Prevent certain blocks from being used in the Gutenberg Editor.

30+
Code Preview
php
<?php
add_filter( 'allowed_block_types_all', function ( $allowed_block_types, $block_editor_context ) {
	// List here the blocks you want to disallow. https://developer.wordpress.org/block-editor/reference-guides/core-blocks/
	$disallowed_blocks = array(
		'core/navigation',
		'core/query',
	);
	if ( ! is_array( $allowed_block_types ) || empty( $allowed_block_types ) ) {
		$registered_blocks   = WP_Block_Type_Registry::get_instance()->get_all_registered();
		$allowed_block_types = array_keys( $registered_blocks );
	}
	$filtered_blocks = array();
	foreach ( $allowed_block_types as $block ) {
		if ( ! in_array( $block, $disallowed_blocks, true ) ) {
			$filtered_blocks[] = $block;
		}
	}
	return $filtered_blocks;
}, 10, 2 );

Comments

Add a Comment