Exciting News! Flipper Code is now WePlugins! Same commitment to WordPress Development excellence, brand new identity.

How to use block_categories_all filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
February 20, 2023
5 minutes read

block_categories_all filter

Filters the default array of categories for block types.

To use the block_categories_all filter, first, you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.

WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_block_categories_all_defaults which takes 2 parameters and we registered using add_filter. The first parameter block_categories_all is the name of the hook, The second parameter modify_block_categories_all_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_all filter.

Parameters

    Below are the 2 parameters required to use this hook.

  • $block_categories : (array[]) Array of categories for block types.
  • $block_editor_context : (WP_Block_Editor_Context) The current block editor context.

Live Example

Access Premium WordPress Plugins

Example 1: Adding a new (custom) block category

This example demonstrates how to add 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 weplugins_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'] ) ) { ... }
    }
    

Example 2: Modify block categories

Below is an example of how you can use this hook to modify block categories.

    function weplugins_modify_block_categories_all_defaults($block_categories, $block_editor_context) { 
        // 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_all", "weplugins_modify_block_categories_all_defaults", 10, 2 );
    

Example 3: Removing a hook callback

To remove a hook callback, use the example below.

    remove_filter( "block_categories_all", "weplugins_modify_block_categories_all_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 customization or are facing any trouble using this hook, please contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.