Using WordPress ‘block_categories_all’ PHP filter

The block_categories_all WordPress PHP filter allows you to modify the default array of categories for block types.

Usage

add_filter('block_categories_all', 'my_custom_block_categories', 10, 2);

function my_custom_block_categories($block_categories, $block_editor_context) {
  // your custom code here
  return $block_categories;
}

Parameters

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

More information

See WordPress Developer Resources: block_categories_all

Examples

Add a new block category

Add a new category named ‘My Custom Category’ to the block categories list.

add_filter('block_categories_all', 'add_my_custom_category', 10, 2);

function add_my_custom_category($block_categories, $block_editor_context) {
  $block_categories[] = array(
    'slug' => 'my-custom-category',
    'title' => __('My Custom Category', 'textdomain'),
    'icon'  => 'wordpress',
  );
  return $block_categories;
}

Remove a block category

Remove the ‘Widgets’ category from the block categories list.

add_filter('block_categories_all', 'remove_widgets_category', 10, 2);

function remove_widgets_category($block_categories, $block_editor_context) {
  $block_categories = array_filter($block_categories, function($category) {
    return $category['slug'] !== 'widgets';
  });
  return $block_categories;
}

Rename a block category

Rename the ‘Design’ category to ‘Layout’.

add_filter('block_categories_all', 'rename_design_category', 10, 2);

function rename_design_category($block_categories, $block_editor_context) {
  foreach ($block_categories as &$category) {
    if ($category['slug'] === 'design') {
      $category['title'] = __('Layout', 'textdomain');
      break;
    }
  }
  return $block_categories;
}

Change the icon of a block category

Change the icon of the ‘Text’ category to ‘editor-text-color’.

add_filter('block_categories_all', 'change_text_category_icon', 10, 2);

function change_text_category_icon($block_categories, $block_editor_context) {
  foreach ($block_categories as &$category) {
    if ($category['slug'] === 'text') {
      $category['icon'] = 'editor-text-color';
      break;
    }
  }
  return $block_categories;
}

Reorder block categories

Move the ‘Media’ category to the top of the block categories list.

add_filter('block_categories_all', 'reorder_media_category', 10, 2);

function reorder_media_category($block_categories, $block_editor_context) {
  $media_category = null;

  foreach ($block_categories as $key => $category) {
    if ($category['slug'] === 'media') {
      $media_category = $category;
      unset($block_categories[$key]);
      break;
    }
  }

  if ($media_category) {
    array_unshift($block_categories, $media_category);
  }

  return $block_categories;
}