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?

  • 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 at 19:49
  • My code is written as it's own little plugin. – LKD Aug 6 at 14:05

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

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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