Disabling Blocks in WordPress with JavaScript

Disable the Navigation and Query blocks

wp.domReady( () => {
    const disallowedBlocks = [
        'core/navigation',
        'core/query',
    ];

    disallowedBlocks.forEach( function( block ) {
        wp.blocks.unregisterBlockType( block );
    });
});

Only allow specific blocks and unregister all others

wp.domReady( () => {
    const allowedBlocks = [
        'core/heading',
        'core/image',
        'core/list',
        'core/paragraph',
    ];

    wp.blocks.getBlockTypes().forEach( function( blockType ) {
        if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
            wp.blocks.unregisterBlockType( blockType.name );
        }
    });
});

Leave the first comment