Using WordPress ‘register_block_type()’ PHP function

The register_block_type() WordPress PHP function registers a block type, using metadata stored in the block.json file.

Usage

register_block_type( $block_type, $args );

Parameters

  • $block_type (string|WP_Block_Type) (Required): Block type name including namespace, or alternatively a path to the JSON file with metadata definition for the block, or a path to the folder where the block.json file is located, or a complete WP_Block_Type instance. If a WP_Block_Type is provided, the $args parameter will be ignored.
  • $args (array) (Optional): Array of block type arguments. Accepts any public property of WP_Block_Type. See WP_Block_Type::__construct() for information on accepted arguments.

More information

See WordPress Developer Resources: register_block_type

Examples

Registering a basic block

This example registers a basic block called “my-plugin/example-block”.

function my_plugin_register_block() {
    register_block_type( 'my-plugin/example-block', array(
        'editor_script' => 'my-plugin-block-editor-script',
    ));
}
add_action( 'init', 'my_plugin_register_block' );

Registering a block with custom category

This example registers a block with a custom category called “my-category”.

function my_plugin_register_block_with_category() {
    register_block_type( 'my-plugin/example-block-category', array(
        'editor_script' => 'my-plugin-block-editor-script',
        'category' => 'my-category',
    ));
}
add_action( 'init', 'my_plugin_register_block_with_category' );

Registering a block with a custom icon

This example registers a block with a custom icon using a dashicon.

function my_plugin_register_block_with_icon() {
    register_block_type( 'my-plugin/example-block-icon', array(
        'editor_script' => 'my-plugin-block-editor-script',
        'icon' => 'dashicons-admin-plugins',
    ));
}
add_action( 'init', 'my_plugin_register_block_with_icon' );

Registering a block with additional keywords

This example registers a block with additional keywords for searching.

function my_plugin_register_block_with_keywords() {
    register_block_type( 'my-plugin/example-block-keywords', array(
        'editor_script' => 'my-plugin-block-editor-script',
        'keywords' => array( 'keyword1', 'keyword2' ),
    ));
}
add_action( 'init', 'my_plugin_register_block_with_keywords' );

Registering a block with a custom style

This example registers a block with a custom editor style.

function my_plugin_register_block_with_style() {
    register_block_type( 'my-plugin/example-block-style', array(
        'editor_script' => 'my-plugin-block-editor-script',
        'editor_style' => 'my-plugin-block-editor-style',
    ));
}
add_action( 'init', 'my_plugin_register_block_with_style' );