Warning: This hook has been deprecated. Use the ‘block_categories_all’ filter instead.

apply_filters_deprecated( 'block_categories', array[] $block_categories, WP_Post $post )

Filters the default array of categories for block types.


Parameters Parameters

$block_categories

(array[]) Array of categories for block types.

$post

(WP_Post) Post being loaded.


Top ↑

Source Source

File: wp-includes/block-editor.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
5.8.0 Use the 'block_categories_all' filter instead.
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Mahdi Yazdani

    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 2 content
    Contributed by Cory Hughart

    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.