Disabling & changing default Gutenberg features
Only allow certain types of blocks (aka ‚remove‘ blocks)
👉 allowed_block_types hook
add_filter( 'allowed_block_types', 'slug_allowed_block_types' );
function slug_allowed_block_types( $allowed_blocks ) {
return array(
'core/image',
'core/paragraph',
'core/heading',
'core/list'
);
}
Takes $post as a second parameter, so the allowed blocks can be set on a per post-type basis
add_filter( 'allowed_block_types', 'slug_allowed_block_types', 10, 2 );
function slug_allowed_block_types( $allowed_blocks, $post ) {
$allowed_blocks = array(
'core/image',
// ...
);
if( $post->post_type === 'page' ) {
$allowed_blocks[] = 'core/shortcode';
}
return $allowed_blocks;
}
Note:
- block list is empty on default
- allowed blocks per template via
if( 'page-template-name.php' == get_page_template_slug( $post ){ ... }but is only aplied when page with template already has been saved. - for block slug see Misha Rudrastyhs collection
Source: Misha Rudrastyh: Removing Default Gutenberg Blocks
Disable default WP block styles
function slug_remove_gutenberg_default_styles() {
if( !is_admin() ){
wp_dequeue_style( 'wp-block-library');
}
}
add_action( 'wp_print_styles', 'slug_remove_gutenberg_default_styles' );
(duplicate of Gutenberg notes entry)
Disable color palettes
// Disable color palette.
add_theme_support( 'editor-color-palette' );
// Disable color picker.
add_theme_support( 'disable-custom-colors' );
Note: As with all add_theme_support methods hook into after_setup_theme
Source: Theme Coder
Block Templates
Predefined blocks per post type.
function slug_add_template_to_page() {
$post_type_object = get_post_type_object( 'page' );
$post_type_object->template = array(
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
array( 'core/image', array(
) ),
);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'slug_add_template_to_page' );
->template-lock:'all': prevents all block manipulation (changing order adding, removing)'insert': prevents inserting or removing blocks but allows chaning the order
See also
- Changing font sizes of the FontSizePicker (note: just calling
add_theme_support( 'editor-font-sizes')removes the Buttons (S,M,L,XL) but leaves the reset button