Using WordPress ‘block_type_metadata’ PHP filter

The block_type_metadata WordPress PHP filter allows you to modify the metadata provided for registering a block type.

Usage

add_filter('block_type_metadata', 'your_custom_function');
function your_custom_function($metadata) {
  // your custom code here
  return $metadata;
}

Parameters

  • $metadata (array): Metadata for registering a block type.

More information

See WordPress Developer Resources: block_type_metadata

Examples

Change Block Category

Update the block category before registering the block.

add_filter('block_type_metadata', 'change_block_category');
function change_block_category($metadata) {
  $metadata['category'] = 'custom_category';
  return $metadata;
}

Add Custom Keywords

Add custom keywords to a block for easier discovery.

add_filter('block_type_metadata', 'add_custom_keywords');
function add_custom_keywords($metadata) {
  $metadata['keywords'][] = 'custom_keyword';
  return $metadata;
}

Modify Block Supports

Alter the features a block supports.

add_filter('block_type_metadata', 'modify_block_supports');
function modify_block_supports($metadata) {
  $metadata['supports']['align'] = ['left', 'right'];
  return $metadata;
}

Add Custom Attributes

Add custom attributes to a block’s metadata.

add_filter('block_type_metadata', 'add_custom_attributes');
function add_custom_attributes($metadata) {
  $metadata['attributes']['custom_attribute'] = array(
    'type' => 'string',
    'default' => 'Hello, world!'
  );
  return $metadata;
}

Change Block Icon

Update the icon for a specific block.

add_filter('block_type_metadata', 'change_block_icon');
function change_block_icon($metadata) {
  if ($metadata['name'] == 'your-plugin/your-block') {
    $metadata['icon'] = 'dashicons-smiley';
  }
  return $metadata;
}