WordPress Custom Block Category and Block Collection: Group Plugin Blocks in the Inserter

Group your blocks in their own inserter category

A plugin with many blocks should not scatter them over “Text”, “Media” and “Design”. Its own category in the inserter keeps them together and makes them easy to find. WordPress 5.8 renamed the filter for it to block_categories_all; the old block_categories still works but is deprecated.

Problem: A plugin registers ten blocks (hero, testimonial, pricing, FAQ and more), and editors cannot find them among the core blocks.

Solution: Add a category with the block_categories_all filter (or block_categories before 5.8), set "category" in each block’s block.json, and optionally register a block collection for the plugin’s namespace.

The category

<?php
function ha_block_category( $categories ) {
    return array_merge(
        array(
            array(
                'slug'  => 'ha-blocks',
                'title' => __( 'HA Blocks', 'textdomain' ),
                'icon'  => null,
            ),
        ),
        $categories
    );
}

if ( function_exists( 'get_default_block_categories' ) ) { // WordPress 5.8+
    add_filter( 'block_categories_all', 'ha_block_category' );
} else {
    add_filter( 'block_categories', 'ha_block_category' );
}

Putting the category first in the array shows it at the top of the inserter. To place it elsewhere, insert it after a given slug instead of prepending it.

The blocks

In each block’s block.json:

{
    "apiVersion": 2,
    "name": "ha/testimonial",
    "title": "Testimonial",
    "category": "ha-blocks",
    "editorScript": "file:./index.js"
}

and register the folder in PHP with register_block_type( __DIR__ . '/blocks/testimonial' ). A block that names a category which does not exist is shown under “Text”, so register the category before relying on it.

A block collection

Collections (WordPress 5.4+) group blocks by namespace: every block whose name starts with ha/ also appears under the collection’s title in the inserter, whatever its category:

wp.domReady( function () {
    wp.blocks.registerBlockCollection( 'ha', { title: 'HA Blocks', icon: 'layout' } );
} );

A category is a place in the inserter; a collection is a label for “all blocks of this plugin”. For a plugin with blocks in several categories (some in “Media”, some in “Widgets”), the collection keeps them findable together.

Fewer blocks for editors

The opposite problem, too many blocks, is solved with the allowed_block_types_all filter (5.8+), which returns the list of block names editors may use, for example per post type.

NOTE: Use a unique slug for the category (ha-blocks, not blocks). Two plugins registering the same slug end up sharing one category, and the title of whichever loads last wins.