Skip to main content

Disable default Gutenberg blocks

disable default gutenberg blocks

With the following code, you can filter and return a small selection of core blocks that you want to use in your theme. Some of the default blocks might not be suitable for your theme or perhaps you want to replace all the native ones your own custom blocks and make it really clear which blocks to choose from.

The second parameter $editor_context is an object of the type WP_Block_Editor_Context which contains information about the block editor that is being rendered. These can be one of:

  • core/edit-post – The post editor at /wp-admin/edit.php.
  • core/edit-widgets – The widgets editor at /wp-admin/widgets.php.
  • core/customize-widgets – The widgets editor at /wp-admin/customize.php.
  • core/edit-site – The site editor at /wp-admin/site-editor.php.

Reference: https://developer.wordpress.org/reference/classes/wp_block_editor_context/

The function first checks that the Editor context post object exists and if so, assumes we’re in the core/edit-post context. Once true the selected array of blocks will be returned. Alternatively you could just return a full array of the block types you want without doing a check.

<?php
function webassembler_allowed_block_types( $allowed_block_types, $editor_context ) {
	if ( ! empty( $editor_context->post ) ) {
		return array(
			'core/paragraph',
			'core/heading',
			'core/list',
		);
	}

	return $allowed_block_types;
}
add_filter( 'allowed_block_types_all', 'webassembler_allowed_block_types', 10, 2 );
Code language: PHP (php)
Privacy Overview

This website uses cookies so that I can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to my website to help me to understand which sections of the website you find most interesting and useful.