In this GitHub space, WordPress team coordinate content to be published on the Developer Blog. Discussion and montly meetings (first Thu) in WP Slack #core-dev-blog
Originally posted by **troychaplin** November 15, 2024
This snippet provides an example of a function to reorder and/or customize the title of the core or custom block categories. In this example the following modifications are happening:
- The title for Text is being updated
- A custom category is being placed after Text
- The embed blocks are being re-positioned below media
```php
/**
* Reorders the block categories in the WordPress editor.
*
* This function allows you to define a custom order for the block categories
* in the WordPress editor, including the ability to set custom titles for
* specific categories. Categories not included in the custom order will be
* appended to the end of the list.
*
* @param array $categories The default block categories.
* @return array The reordered block categories.
*/
function reorder_block_categories( $categories ) {
// Define the new category order with custom titles where needed.
$new_category_order = [
[ 'slug' => 'text', 'title' => 'Text Elements' ],
[ 'slug' => 'custom-category' ],
[ 'slug' => 'media' ],
[ 'slug' => 'embed' ],
[ 'slug' => 'design', 'title' => 'Design & Layout' ],
];
// Get the default titles and slugs for the core block categories.
$get_core_block_categories = array_column( $categories, 'title', 'slug' );
// Check if the new category order has custom titles, fallback to default titles.
foreach ( $new_category_order as &$new_category ) {
$new_category['title'] = $new_category['title'] ?? $get_core_block_categories[ $new_category['slug'] ] ?? 'Untitled';
}
// Create an array of slugs from the new category order.
$new_category_slugs = array_column( $new_category_order, 'slug' );
// Filter out the remaining block categories that are not in the new order.
$remaining_core_categories = array_filter( $categories, function ( $category ) use ( $new_category_slugs ) {
return ! in_array( $category['slug'], $new_category_slugs, true );
});
// Merge the new category order with the remaining categories.
return array_merge( $new_category_order, $remaining_core_categories );
}
add_filter( 'block_categories_all', 'example_block_category', 10, 2 );
```
@troychaplin The Snippet format is not yet particular finalized as we just started working on it, but it's meant to cover a single purpose/task, people might want to do.
So if that's more elaborate you might consider it for a full tutorial, or split it up into multiple snippets.
Thanks @bph, I'm gonna work out the individual functions as I want a reorder function for another project but don't need the renaming. If @justintadlock would prefer them as individual functions I'll be prepared for that. Personally, I think it might be best as 2 functions. I could see reordering being desired more than renaming.
@justintadlock what's your opinion on one large function vs individual functions. I did a bit of work on these and ended up keeping the one function for my own personal use as it will register, reorder or rename all in one, it's just easier as an all-in-one.
Discussed in https://github.com/WordPress/developer-blog-content/discussions/340
@troychaplin The Snippet format is not yet particular finalized as we just started working on it, but it's meant to cover a single purpose/task, people might want to do. So if that's more elaborate you might consider it for a full tutorial, or split it up into multiple snippets.
Also cc: @justintadlock to assist.
Thanks @bph, I'm gonna work out the individual functions as I want a reorder function for another project but don't need the renaming. If @justintadlock would prefer them as individual functions I'll be prepared for that. Personally, I think it might be best as 2 functions. I could see reordering being desired more than renaming.
@justintadlock what's your opinion on one large function vs individual functions. I did a bit of work on these and ended up keeping the one function for my own personal use as it will register, reorder or rename all in one, it's just easier as an all-in-one.