Last active
October 25, 2024 08:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was just looking for something like this, unfortunately the array_diff fails with the first arg, as it defaults to true.