apply_filters_deprecated( ‘block_categories’, array[] $block_categories, WP_Post $post )

This hook has been deprecated. Use the {@see ‘block_categories_all’} filter instead.

Filters the default array of categories for block types.

Parameters

$block_categoriesarray[]
Array of categories for block types.
$postWP_Post
Post being loaded.

Source

$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );

Changelog

VersionDescription
5.8.0Use the 'block_categories_all' filter instead.
5.0.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Register a new block category for “My Plugin” if it doesn’t exist already.

    Note: You can also pass a second argument $post to generate a different category depending on the post’s content or type.

    /**
     * Creating a new (custm) block category.
     *
     * @param   array $categories     List of block categories.
     * @return  array
     */
    function wpdocs_new_block_category( $categories ) {
        // Plugin’s block category title and slug.
        $block_category = array( 'title' => esc_html__( 'My Plugin', 'text-domain' ), 'slug' => 'myplugin' );
        $category_slugs = wp_list_pluck( $categories, 'slug' );
    
        if ( ! in_array( $block_category['slug'], $category_slugs, true ) ) {
            $categories = array_merge(
                $categories,
                array(
                    array(
                        'title' => $block_category['title'], // Required
                        'slug'  => $block_category['slug'], // Required
                        'icon'  => 'wordpress', // Slug of a WordPress Dashicon or custom SVG
                    ),
                )
            );
        }
    
        return $categories;
    }
    add_filter( 'block_categories', 'wpdocs_new_block_category' );
  2. Skip to note 4 content

    If you’ve come here because you’re getting deprecation notices about `block_categories`, as of 5.8 this filter is deprecated. Use `block_categories_all` instead.

    If you need to apply categories based on post type or other $post data, you can do so like this:

    /**
     * Create a custom block category for posts only.
     *
     * @param   array $categories     List of block categories.
     * @return  array
     */
    function wpdocs_post_block_category( $categories, $editor_context ) {
    	if ( $editor_context->post instanceof WP_Post && 'post' === $editor_context->post->post_type ) {
    		$categories = array_merge(
    			$categories,
    			array(
    				'title' => __( 'Title' ), // Required
    				'slug'  => 'slug', // Required
    				'icon'  => 'wordpress', // Slug of a WordPress Dashicon or custom SVG
    			)
    		);
    	}
    
    	return $categories;
    }
    add_filter( 'block_categories_all', 'wpdocs_post_block_category' );

You must log in before being able to contribute a note or feedback.