8

I'm trying to unregister core block types in WordPress Gutenberg.

I've used the code provided here: https://github.com/WordPress/gutenberg/blob/master/docs/extensibility/extending-blocks.md#removing-blocks

But I can't get it to work. I feel like there may be a simple step I am missing?

  1. I created a plugin which I activated in my WP theme.
  2. In my plugin [clore-blocks] folder I created "clore-blocks.php". It contains:
/**
 * Remove certain blocks.
 */
function clore_blacklist_blocks() {
    wp_enqueue_script(
        'clore-blacklist-blocks',
        plugins_url( 'blocks.js', __FILE__ ),
        array( 'wp-blocks' ),
        filemtime( plugin_dir_path( __FILE__ ) . 'blocks.js' ) // Version: filemtime - Gets file modification time.
    );
}
add_action( 'enqueue_block_editor_assets', 'clore_blacklist_blocks' );
  1. In blocks.js I have only:
wp.blocks.unregisterBlockType( 'core/verse' );

I thought this would remove the "Verse" block type from being used, but when I go to edit a page with the Gutenberg editor, "Verse" is still there.

When I view the page source from the WP edit page, I can see that my "blocks.js" file is being referenced correctly, but It's just not doing anything... or at least not what I want it to do. Do you have any idea why that is?

3
  • 1
    What do you mean by "a plugin which I activated in my WP theme?" Is your code a plugin, or a theme?
    – WebElaine
    Aug 3, 2018 at 19:49
  • My code is written as it's own little plugin.
    – LKD
    Aug 6, 2018 at 14:05
  • I am having the same issue. When I do wp.blocks.unregisterBlockType( 'core/verse' ) (or any other core block type) I get an error in the console that this block type is not registered.
    – JakeParis
    Nov 13, 2018 at 16:13

3 Answers 3

4

Everything works for me with allowed_block_types hook.

Example:

add_filter( 'allowed_block_types', 'my_function' );

function my_function( $allowed_block_types ) {

    return array(
        'core/paragraph'
    );

}

You can insert the above code to your functions.php file to a custom plugin. It removes all blocks except the Paragraph block.

More examples here https://rudrastyh.com/gutenberg/remove-default-blocks.html

4
  • 3
    This is not always a good solution because it removes all blocks except for a whitelist.
    – JakeParis
    Nov 13, 2018 at 16:11
  • This isn't the answer the OP was after, but I think it solves the same problem the other way around. As @JakeParis noted, it creates a approved list of blocks instead of removing them one-by-one and this is exactly what I need in my case—just didn't know it until now.
    – phip
    Feb 4, 2020 at 3:44
  • This hook is currently WP 6.2 deprecated - the recommended one is allowed_block_types_all
    – Picard
    Apr 11 at 20:33
  • Here's the approach using the new allowed_block_types_all hook. wordpress.stackexchange.com/a/416581/29136 Jun 7 at 15:19
3

Had the same problem following the code suggestion in the Gutenberg docs.

I solved this by adding a wp.domReady() call to the JavaScript code. So, like this:

wp.domReady( function() {
    wp.blocks.unregisterBlockType( 'core/verse' );
});

Now, the 'verse' block is gone from the block editor.

I think this has to do with the order of loading javascript files in the admin. The unregisterBlockType should be called after the block editor has loaded.

Hope this helps!

Thanks to PayteR for the suggestion and see https://wordpress.org/gutenberg/handbook/designers-developers/developers/filters/block-filters/#using-a-blacklist for more info.

2
  • This answer is not correct. Correct way is to use wp.domReady() even hook, like it's eplained here wordpress.org/gutenberg/handbook/designers-developers/…
    – PayteR
    Apr 25, 2019 at 2:44
  • Thanks for the suggestion @PayteR. That looks like the piece of documentation i needed when solving this myself. I've edited my answer.
    – Fat Pixel
    Apr 26, 2019 at 7:30
0

Here's another approach using PHP hook on how to disable specific block on the Gutenberg editor:

<?php
function disable_specific_blocks( $allowed_block_types, $post ) {
    // 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 );
?>

You can check this guide on how to restrict specific blocks on the WordPress block editor for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.