Filters the default array of categories for block types.
Parameters
$block_categoriesarray[]- Array of categories for block types.
$block_editor_contextWP_Block_Editor_Context- The current block editor context.
Source
$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |
Adding a new (custom) block category and show that category at the top
Adding a new (custom) block category.
Just a note that in base version WP 5.8,
$block_editor_contextcan be either anobjector astring. If you are using this filter and type hinting your callback, this could be the cause of the errors you’re undoubtedly seeing.Using @heldervilela code above as an example, you may need to do this:
This snippet allows you to add block categories on WP 5.8 and below.
block_categoriesfilter passes aWP_Postinstance as 2nd argument, the filter handler should check for that, as it currently always expects a `WP_Block_Editor_Context` instance of the newblock_categories_allfilter. The documentation recommends to check for the existence of theWP_Block_Editor_Contextclass as feature detection for the newerblock_categories_allfilter, see https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#block_categories_allblock_categories_allfilter passes as 2nd parameter aWP_Block_Editor_Contextinstead of aWP_Postinstance. The post can be accessed asWP_Block_Editor_Contextpostproperty. Therefore your filter handler should check whether the 2nd parameter is aWP_Postor aWP_Block_Editor_Contextso the post instance is correctly retrieved for further usage. The documentation recommends to check for the existence of theWP_Block_Editor_Contextclass as feature detection for the newerblock_categories_allfilter, see https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#block_categories_allDeclare a block category just once, or it will throw an error. It is useful to know if you split your code in several plugins.