How to remove WordPress core blocks (Gutenberg)

Let’s say you wan’t to make WordPress site with only custom blocks and you don’t want core blocks hanging around there confusing editors. There are (least) two ways of removing them.

You can either specify which of core blocks you want to remove or just remove them all at once. Later approach takes in consideration also blocks that may be added core later.

In both cases you will use filter hook ‘allowed_blocks’.

Add one of following codes to your functions.php. With first one list blocks you want to remove. You can see what blocks are there by logging $registered_blocks.

Remove core blocks individually

<?php 

function remove_default_blocks($allowed_blocks){
    // Get all registered blocks
    $registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();

    // Disable default Blocks you want to remove individually
    unset($registered_blocks['core/calendar']);
    unset($registered_blocks['core/legacy-widget']);
    unset($registered_blocks['core/rss']);
    unset($registered_blocks['core/search']);
    unset($registered_blocks['core/tag-cloud']);


    // Get keys from array
    $registered_blocks = array_keys($registered_blocks);

    // Merge allowed core blocks with plugins blocks
    return $registered_blocks;
}

add_filter('allowed_block_types', 'remove_default_blocks');

 

Remove all core blocks

<?php 

function remove_default_blocks($allowed_blocks){
 
    // Get all registered blocks
    $registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
    
    // Remove all blocks that are prefixed with core/
    $filtered_blocks = array();
    foreach($registered_blocks as $block) {
     
      if(strpos($block->name , 'core/') === false) { 
        array_push($filtered_blocks, $block->name);
      }
    }
    
    return $filtered_blocks;
}

add_filter('allowed_block_types', 'remove_default_blocks');

 

Copyright 2020 © Juha Stenroos