Block Editor PHP Beginner

Add a Custom Block Category

Add your own category to the block inserter, ready to hold the custom blocks you build for a site.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 12 lines
add_filter( 'block_categories_all', function ( array $categories ): array {
	return array_merge(
		[
			[
				'slug'  => 'nahnu-blocks',
				'title' => 'Site blocks',
				'icon'  => null,
			],
		],
		$categories
	);
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Custom blocks get lost among the core categories. Putting them in their own category, at the top of the inserter, makes them easy to find.

How it works

The block_categories_all filter returns the inserter’s categories in order. The snippet adds one at the start. Blocks join it by setting "category": "nahnu-blocks" in their block.json.

Things to watch

  • Empty categories don’t show in the inserter, so you won’t see it until a block uses it.
  • For pattern categories, use register_block_pattern_category() instead.

How to test it

Register a block with the new category and open the inserter. “Site blocks” should appear first.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only