Skip to content

Instantly share code, notes, and snippets.

@phpbits
Last active October 24, 2023 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpbits/15f7fbe7446adba65904e2238368d557 to your computer and use it in GitHub Desktop.
Save phpbits/15f7fbe7446adba65904e2238368d557 to your computer and use it in GitHub Desktop.
How to Disable Specific Blocks in the WordPress Gutenberg Block Editor using the allowed_block_types_all Hook
<?php
function disable_specific_blocks( $allowed_block_types, $post ) {
// Make sure allowed types is an array
if ( ! is_array( $allowed_block_types ) ) {
$allowed_block_types = array(
'core/block', // Required to enable "Reusable blocks" feature.
'core/columns',
'core/paragraph',
'core/image',
'core/heading',
'core/gallery',
'core/list',
'core/list-item',
'core/pullquote',
'core/audio',
'core/button',
'core/buttons',
'core/file',
'core/group',
'core/separator',
'core/media-text',
'core/freeform',
'core/embed',
'core/spacer',
);
}
// An array of block names to disable
$disabled_blocks = array(
'core/table',
'core/image',
'core/gallery',
);
// Use this condition to disable blocks on certain post types, otherwise you can remove this IF condition
if ( $post->post_type === 'post' ) {
$allowed_block_types = array_diff( $allowed_block_types, $disabled_blocks );
}
return $allowed_block_types;
}
add_filter( 'allowed_block_types_all', 'disable_specific_blocks', 10, 2 );
?>
@mediaformat
Copy link

I was just looking for something like this, unfortunately the array_diff fails with the first arg, as it defaults to true.

$allowed_block_types bool|string[]
Array of block type slugs, or boolean to enable/disable all.
Default true (all registered block types supported).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment