I am trying to add a new category for some custom blocks using the block_categories_all filter.
I am using TwentyTwentyOne theme and putting the code below into the functions.php file. No plugins at all are installed.
When I dump $new_cats the array_merge has been successful but throws header already sent warning.
function wpdocs_add_new_block_category( $block_categories ) {
$new_cats = array_merge(
$block_categories,
[
[
'slug' => 'my-block-category',
'title' => esc_html__( 'My Block Category', 'text-domain' ),
'icon' => 'wordpress', // Slug of a WordPress Dashicon or custom SVG
],
]
);
# var_dump($new_cats);
return ($new_cats);
}
add_filter( 'block_categories_all', 'wpdocs_add_new_block_category', 10, 2 );
There must be something simple I am missing here??
2
Answers
Maybe you need add to check context, for example:
More information is here
There is nothing wrong in your
add_filter( 'block_categories_all', 'wpdocs_add_new_block_category', 10, 2 );
call.Block categories are not shown if the category has no blocks in it. Add a new block on that category and you will see your block category.