This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Yo, fellow developers! Today we’re diving into the block_categories filter. This nifty filter lets you tweak the default array of categories for block types in WordPress. Whether you’re adding custom blocks or just want to reorganize things, this hook is your friend. Just a heads up, we at WePlugins love creating custom plugins for this stuff to keep things smooth during theme updates.
block_categories filter
Filters the default array of categories for block types.
To use the block_categories filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
In the below live example, we have defined a function modify_block_categories_defaults which takes 2 parameters and we registered using add_filter. The first parameter block_categories is the name of the hook, the second parameter modify_block_categories_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook, so you can use remove_filter to remove the block_categories filter.
Parameters
Below are the 2 parameters required to use this hook:
- $block_categories: (array[]) Array of categories for block types.
- $post: (WP_Post) Post being loaded.
Live Example 1: Creating a New Block Category
Here’s how you can create a new (custom) block category.
/** * Creating a new (custm) block category. * * @param array $categories List of block categories. * @return array */ function weplugins_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( 'myplugin', $category_slugs, true ) ) { $categories[] = $block_category; } return $categories; } add_filter( 'block_categories', 'weplugins_new_block_category' );
Live Example 2: Modifying Block Categories
Below is an example of how you can use this hook to modify block categories.
function weplugins_modify_block_categories_defaults($block_categories, $post) { // Update the $block_categories variable according to your website requirements and return this variable. You can modify the $block_categories variable conditionally too if you want. return $block_categories; } // add the filter add_filter( 'block_categories', 'weplugins_modify_block_categories_defaults', 10, 2 );
Live Example 3: Removing a Hook Callback
To remove a hook callback, use the example below.
remove_filter( 'block_categories', 'weplugins_modify_block_categories_defaults', 10, 2 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you need any customization or run into issues, don’t hesitate to contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.