Filters the default array of categories for block types.
Parameters
$block_categories
array[]- Array of categories for block types.
$block_editor_context
WP_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_context
can be either anobject
or 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_categories
filter passes aWP_Post
instance 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_all
filter. The documentation recommends to check for the existence of theWP_Block_Editor_Context
class as feature detection for the newerblock_categories_all
filter, see https://developer.wordpress.org/block-editor/reference-guides/filters/block-filters/#block_categories_allblock_categories_all
filter passes as 2nd parameter aWP_Block_Editor_Context
instead of aWP_Post
instance. The post can be accessed asWP_Block_Editor_Context
post
property. Therefore your filter handler should check whether the 2nd parameter is aWP_Post
or aWP_Block_Editor_Context
so the post instance is correctly retrieved for further usage. The documentation recommends to check for the existence of theWP_Block_Editor_Context
class as feature detection for the newerblock_categories_all
filter, 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.