The block category in Gutenberg is a feature that allows organizing and grouping the various blocks available in the WordPress block editor. It provides a logical and intuitive structure for users when they are searching for and selecting blocks to build their pages or posts.
Why Create a New Category?
Creating a new category for Gutenberg blocks in WordPress can be interesting for several reasons:
- Organization: Having specific categories for your blocks helps better organize the Gutenberg editor. It makes it easier to find relevant blocks, especially if you have a large number of custom blocks.
- Clarity: By creating specific categories, you can provide clearer meaning to the purpose of each block, helping users quickly understand the usefulness of each available block.
- Customization: If you’re developing blocks for a specific theme or plugin, grouping them into a custom category makes the user interface more consistent and aligned with the theme or plugin’s functionality.
Default Block Categories
Gutenberg provides a series of default categories for organizing the available blocks. These categories offer a basic structure for classifying blocks based on their use and content type. Here are some of the most common default categories:
- Text
The Text category blocks are used for adding and formatting textual content. It includes paragraph, heading, list, quote blocks, etc., which enable composing and styling text effectively. - Media
The Media category groups blocks that allow adding and manipulating media such as images, galleries, videos, and audio files. These blocks provide a simple and intuitive way to integrate different types of media into content. - Design
This category groups blocks that help structure the layout of a page or post. It typically includes column, group, and container blocks that facilitate creating complex layouts. - Widgets
Widgets category blocks are often used to add interactive or dynamic elements to a page or post. It includes blocks like buttons, contact forms, counters, icons, etc., which enrich the user experience. - Theme
This category groups blocks specific to a given WordPress theme. These blocks are designed to work optimally with the active theme on the WordPress site, providing visual and functional consistency in layout design. - Embedded Content
This category groups blocks that allow embedding content from external sources such as social networks, video platforms, and mapping services. It includes blocks for embedding tweets, YouTube videos, Google Maps, etc.
Adding the Category in PHP
To create a new Gutenberg block category, you need to use the block_categories
filter (or block_categories_all
for newer versions of WordPress) in your plugin or theme file. Here’s an example code to add a new category:
When registering your blocks, you can assign them to the new category by specifying the category in the block configuration object.
Code Explanation
block_categories_all
Filter (formerlyblock_categories
): This filter allows modifying or adding block categories in the Gutenberg editor. It takes two arguments: the existing categories and the current post.my_custom_block_categories
Function: This function adds a new category to the list of existing categories. The new category has a slug, a title, and an optional icon.- Block Registration: When registering a new block with
registerBlockType
, you specify the category using the slug of the custom category.
By following these steps, you can create a new category for your Gutenberg blocks, improving organization and user experience in the editor.