block_categoriesfilter-hookWP 5.0.0

Deprecated since 5.8.0. It is no longer supported and may be removed in future releases. See block_categories_all.

Allows you to add/remove/change Gutenberg block categories.

For a new category to appear in the menu, at least one block must be added to it!

Usage

add_filter( 'block_categories', 'wp_kama_block_categories_filter', 10, 3 );

/**
 * Function for `block_categories` filter-hook.
 * 
 * @param array[] $block_categories Array of categories for block types.
 * @param WP_Post $post             Post being loaded.
 * @param         $string           
 *
 * @return array[]
 */
function wp_kama_block_categories_filter( $block_categories, $post, $string ){

	// filter...
	return $block_categories;
}
$default_categories(array)
An array of block categories.
$post(object)
The currently loaded post.

Examples

#1 Add a new category to group custom blocks

add_filter( 'block_categories', 'custom_block_category', 10, 2 );

function custom_block_category( $default_categories, $post ) {

	if ( $post->post_type !== 'post' ) {
		return $default_categories;
	}

	return array_merge(
		$default_categories,
		[
			[
				'slug'  => 'glum-category',     // Category slug to use when registering the block
				'title' => __( 'Glum Category', 'my-plugin' ),      // Displayed category name
				'icon'  => 'wordpress'      // Category icon; null can be passed if no icon is needed
			],
		]
	);

}

Changelog

Since 5.0.0 Introduced.
Deprecated since 5.8.0 Use the {@see 'block_categories_all'} filter instead.

Where the hook is called

get_block_categories()
block_categories
wp-includes/block-editor.php 100
$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );

Where the hook is used in WordPress

Usage not found.