5

Anyone know how to re-arrange, manipulate the Block Categories in the Gutenberg Editor in Wordpress I can't even return a list of them as you can with the Blocks themselves, all I can find is 'getCategories' which doesn't seem do anything... the new documentation is not great at all.

  • I second this, all I can find is the get_block_categories() hook - https://developer.wordpress.org/reference/functions/get_block_categories/, but all it does is list the available blocks, can't seem to find a way to manipulate it at all. – Paul Jackson May 11 '19 at 22:25

2 Answers2

2

This did the trick for me to register a custom Guttenberg block and make it the first option in WP Admin editor:

function custom_block_category( $categories ) {
    $custom_block = array(
        'slug'  => 'my-blocks',
        'title' => __( 'My Test Blocks', 'my-blocks' ),
    );

    $categories_sorted = array();
    $categories_sorted[0] = $custom_block;

    foreach ($categories as $category) {
        $categories_sorted[] = $category;
    }

    return $categories_sorted;
}
add_filter( 'block_categories', 'custom_block_category', 10, 2);
ecairol
  • 5,166
  • 1
  • 20
  • 19
0

Here's a shorter solution:

function custom_block_category( $categories ) {
    return array_merge(
        array(
            array(
                'slug' => 'my-blocks',
                'title' => __( 'My Test Blocks', 'my-blocks' ),
            ),
        ),
        $categories
    );
}
add_filter( 'block_categories', 'custom_block_category', 10, 2 );
Enso
  • 1
  • This removes any other category from the list. – Jsalinas Nov 16 '20 at 00:06
  • Are you sure? It looks plain simple and should basically append the new category to the existing ones. I also just checked it. Prior to it, it's 6 categories and afterwards 7 categories. Also, it's basically the same approach as listed in the WordPress dev handbook (https://developer.wordpress.org/block-editor/developers/filters/block-filters/#managing-block-categories), but just changing the order and thus inserting the own category at the front. – Enso Nov 17 '20 at 06:13