apply_filters( 'block_categories', array[] $default_categories, WP_Post $post )

Filter the default array of block categories.


Parameters Parameters

$default_categories

(array[]) Array of block categories.

$post

(WP_Post) Post being loaded.


Top ↑

Source Source

File: wp-admin/includes/post.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
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' );
    

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