apply_filters( 'block_categories_all', array[] $block_categories, WP_Block_Editor_Context $block_editor_context )

Filters the default array of categories for block types.


Parameters Parameters

$block_categories

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

$block_editor_context

(WP_Block_Editor_Context) The current block editor context.


Top ↑

Source Source

File: wp-includes/block-editor.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
5.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Helder Vilela

    Adding a new (custom) block category.

    /**
     * Adding a new (custom) block category.
     *
     * @param   array $block_categories                         Array of categories for block types.
     * @param   WP_Block_Editor_Context $block_editor_context 	The current block editor context.
     */
    function wpdocs_add_new_block_category( $block_categories, $block_editor_context ) {
    	// You can add extra validation such as seeing which post type
    	// is used to only include categories in some post types.
    	// if ( in_array( $block_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }
    
    	return 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
    			],
    		]
    	);
    }
    
    add_filter( 'block_categories_all', 'wpdocs_add_new_block_category' );
    
  2. Skip to note 2 content
    Contributed by Michael Bourne

    Just a note that in base version WP 5.8, $block_editor_context can be either an object or a string. If you are using this filter and type hinting your callback, this could be the cause of the errors you’re undoubtedly seeing.

    Using @heldervilela code above as an example, you may need to do this:

    /**
     * Adding a new (custom) block category.
     *
     * @param   array $block_categories                                Array of categories for block types.
     * @param   WP_Block_Editor_Context|string $block_editor_context   The current block editor context, or a string defining the context.
     */
    function wpdocs_add_new_block_category( $block_categories, $block_editor_context ) {
        // Check the context of this filter, return default if not in the post/page block editor.
        // Alternatively, use this check to add custom categories to only the customizer or widget screens.
        if ( ! ( $block_editor_context instanceof WP_Block_Editor_Context ) ) {
            return $block_categories;
        }
    
        // You can add extra validation such as seeing which post type
        // is used to only include categories in some post types.
        // if ( in_array( $block_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }
     
        return 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
                ],
            ]
        );
    }
     
    add_filter( 'block_categories_all', 'wpdocs_add_new_block_category' );
  3. Skip to note 3 content
    Contributed by Ronald Huereca

    This snippet allows you to add block categories on WP 5.8 and below.

    if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) {
    	add_filter( 'block_categories', 'wpdocs_add_block_category', 10, 2 );
    } else {
    	add_filter( 'block_categories_all', 'wpdocs_add_block_category', 10, 2 );
    }
    
    /**
     * Adding a new (custom) block category.
     *
     * @param   array                   $block_categories       Array of categories for block types.
     * @param   WP_Block_Editor_Context $block_editor_context   The current block editor context.
     */
    function wpdocs_add_block_category( $block_categories, $block_editor_context ) {
    	return array_merge(
    		$block_categories,
    		array(
    			array(
    				'slug'  => 'block-slug',
    				'title' => __( 'Block Category Title', 'text-domain' ),
    			),
    		)
    	);
    }
    

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