The block_categories WordPress PHP filter modifies the default array of categories for block types.
Usage
add_filter('block_categories', 'my_custom_block_categories', 10, 2);
function my_custom_block_categories($block_categories, $post) {
// Your custom code here
return $block_categories;
}
Parameters
$block_categories(array): Array of categories for block types.$post(WP_Post): Post being loaded.
More information
See WordPress Developer Resources: block_categories
Examples
Add a new category to block categories
Add a new custom category named “My Custom Category” to the block categories list.
add_filter('block_categories', 'add_my_custom_block_category', 10, 2);
function add_my_custom_block_category($block_categories, $post) {
$block_categories[] = array(
'slug' => 'my-custom-category',
'title' => __('My Custom Category', 'textdomain'),
);
return $block_categories;
}
Remove a specific category from block categories
Remove the “Widgets” category from the block categories list.
add_filter('block_categories', 'remove_widgets_category', 10, 2);
function remove_widgets_category($block_categories, $post) {
$block_categories = array_filter($block_categories, function ($category) {
return $category['slug'] !== 'widgets';
});
return $block_categories;
}
Rename a specific category in block categories
Rename the “Common” category to “Frequently Used” in the block categories list.
add_filter('block_categories', 'rename_common_category', 10, 2);
function rename_common_category($block_categories, $post) {
foreach ($block_categories as &$category) {
if ($category['slug'] === 'common') {
$category['title'] = __('Frequently Used', 'textdomain');
}
}
return $block_categories;
}
Change the order of block categories
Move the “Layout” category to the top of the block categories list.
add_filter('block_categories', 'move_layout_category_to_top', 10, 2);
function move_layout_category_to_top($block_categories, $post) {
usort($block_categories, function ($a, $b) {
return $a['slug'] === 'layout' ? -1 : ($b['slug'] === 'layout' ? 1 : 0);
});
return $block_categories;
}
Conditionally add a new category for a specific post type
Add a new custom category named “Product Blocks” only for the “product” post type.
add_filter('block_categories', 'add_product_blocks_category', 10, 2);
function add_product_blocks_category($block_categories, $post) {
if ($post->post_type === 'product') {
$block_categories[] = array(
'slug' => 'product-blocks',
'title' => __('Product Blocks', 'textdomain'),
);
}
return $block_categories;
}