register_block_type()WP 5.0.0

Registers a new block type for the block editor (Gutenberg).

For correct registration of the block, the $block_type field must be identical in both php and js functions:

register_block_type( 'alias/example', [] );
registerBlockType( 'alias/example', {} )

See also the description of the JS function registerBlockType() many of the parameters of this function are passed there.

Usually, this function is triggered on the init event.

No Hooks.

Returns

WP_Block_Type|false. The registered block object on success or false on error.

Usage

register_block_type( $block_type, $args );
$block_type(string/WP_Block_Type) (required)

The name of the block, including the namespace or a full instance of WP_Block_Type. The $args parameter is not needed when an instance of WP_Block_Type{} is specified (it will be ignored).

You can specify the path to the block.json file of the block:

register_block_type( __DIR__ . '/block.json' );

Or you can specify the path to the folder where the block.json file is located:

register_block_type( __DIR__ . '/my-block' );
$args(array)

An array of block arguments. Accepts any public property of the WP_Block_Type class. Possible parameters can be found in WP_Block_Type::__construct().

  • api_version(string)
    Version of the block API.

  • title(string)
    Block label.

  • category(string|null)
    Block category for classification. Used in the search interface to place blocks by categories.

  • parent(string[]|null)
    Setting a parent allows the block to require being nested only within specified blocks.

  • ancestor(string[]|null)
    Setting an ancestor makes the block available only within specified blocks at any position under the ancestor block's subtree.

  • icon(string|null)
    Block type icon. You can specify a string, the name of a dashicon, without the dashicon- prefix. For example dashicons-admin-links >>> admin-links.

  • description(string)
    Detailed description of the block.

  • keywords(string[])
    Additional keywords for displaying the block in search results.

  • textdomain(string|null)
    Text domain for translation.

  • styles(array[])
    Alternative styles for the block.

  • variations(array[])
    Block variations.

  • selectors(array)
    Custom CSS selectors for generating styles in theme.json.

  • supports(array|null)
    Supported features.

  • example(array|null)
    Structured data for block preview.

  • render_callback(callable|null)
    The function used to render the block content. Accepts parameters $attributes and $content.

  • attributes(array|null)
    Schemas of the block attribute properties.

  • uses_context(string[])
    Context values inherited by blocks of this type.

  • provides_context(string[]|null)
    Context provided by blocks of this type.

  • editor_script_handles(string[])
    Script handles only for the block editor.

  • script_handles(string[])
    Script handles for front-end and block editor.

  • view_script_handles(string[])
    Script handles only for the front-end of the block.

  • editor_style_handles(string[])
    Style handles only for the block editor.

  • style_handles(string[])
    Style handles for front-end and block editor.

Default: array()

Examples

4

#1 Register a new block type with additional parameters

This example shows how to register a block in a class context. The block will output the posts.

new Gutenberg_Block_Example(); // initialize

class Gutenberg_Block_Example {

	public function __construct() {
		add_action( 'init', [ $this, 'gutenberg_block_register_block' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'gutenberg_block_editor_scripts' ] );
	}

	public function gutenberg_block_register_block() {

		register_block_type( 'gutenberg-block/example', [
			'render_callback' => [ $this, 'gutenberg_block_render_callback' ],
			'attributes'      => [
				'posts_per_page' => [
					'type'    => 'number',
					'default' => 3,
				],
				'order'        => [
					'type'    => 'string',
					'default' => 'desc',
				],
			],

		] );
	}

	public function gutenberg_block_editor_scripts() {

		wp_register_script(
			'example',
			plugins_url( 'build/index.js', __FILE__ ),
			['wp-blocks']
		);

		wp_enqueue_script( 'example' );

	}

	public function gutenberg_block_render_callback( $attributes, $content ) {

		$args = [
			'posts_per_page' => $attributes['postsPerPage'],
			'post_status'    => 'publish',
			'order'          => $attributes['order'],
		];

		$posts = get_posts( $args );

		$html = '<div>';

		if ( $posts ) {
			$html .= '<ul>';

			foreach ( $posts as $item ) {
				$html .= '<li><a href="' . get_the_permalink( $item->ID ) . '">' . $item->post_title . '</a></li>';
			}

			$html .= '</ul>';
		} else {
			$html .= '<h3>' . __( 'No posts!', 'gutenberg-blocks' ) . '</h3>';
		}

		$html .= '</div>';

		return $html;

	}

}
0

#2 Register a new block type without additional parameters

add_action( 'init', 'gutenberg_block_register_block' );
add_action( 'enqueue_block_editor_assets', 'gutenberg_block_editor_scripts' );

// Register the new side type
function gutenberg_block_register_block() {
	register_block_type( 'gutenberg-block/example', [] );
}

// Register the main script for the block
function gutenberg_block_editor_scripts() {
	wp_register_script(
		'example',
		plugins_url( 'build/index.js', __FILE__ ),
		['wp-blocks']
	);

	wp_enqueue_script( 'example' );
}
0

#3 Pass custom $attributes

You can pass your attributes $attributes, which can be used both in the editor and on the front-end in render_callback:

register_block_type( 'my_namespace/my_block', [
	'render_callback' => 'render_callback',
	'attributes'      => [
		'some_string' => [
			'default' => 'default string',
			'type'    => 'string'
		],
		'some_array'  => [
			'type'  => 'array',
			'items' => [
				'type' => 'string',
			],
		]
	],
	'render_callback' => 'render_block_my_custom_blocks_calendar',
	'editor_script'   => 'calendar-editor-js',
	'editor_style'    => 'calendar-editor-css',
	'script'          => 'calendar-frontend-js',
	'style'           => 'calendar-frontend-css',
] );

Important (tested in 5.0.3): It is mandatory to specify the parameter type, otherwise a notice will be issued.

0

#4 Autocreation of blocks via .json files

class My_Blocks {

	public function setup_hooks(): void {
		add_action( 'acf/init', [ $this, 'register_blocks' ] );
		add_filter( 'block_categories_all', [ $this, 'register_block_category' ] );
	}

	public function register_blocks(): void {

		$blocks = glob( __DIR__ . '/Blocks/*/block.json');

		if ( ! $blocks ) {
			return;
		}

		foreach ( $blocks as $block ) {
			register_block_type( $block );
		}
	}

}

( new My_Blocks() )->setup_hooks();

Example .json file:

{
  "name": "ice-cream/slider",
  "title": "Ice Cream Slider",
  { "description": "Simple customizable image slider",
  "style": "block.css", 
  "category": "ice-cream",
  "icon": "images-alt",
  "apiVersion": 2,
  "keywords": [],
  "acf": {
	"mode": "preview",
	"renderTemplate": "render.php"
  },
  "styles": [],
  "supports": {
	"align": false,
	"anchor": false,
	"alignContent": false,
	"color": {
	  "text": false,
	  "background": true,
	  "link": false
	},
	"alignText": false,
	"fullHeight": false
  }
}

Read more here: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/

0

#5 Using dashicon for the block

To do this, specify an icon without the prefix dashicons- in $args in the parameter icon:

add_action( 'init', 'wpkama_register_block' );

function wpkama_register_block(){
	register_block_type(
		__DIR__ . '/build.json',
		[
			'icon' => 'admin-home', /* omit 'dashicons-' prefix */
		]
	);
}

All Dashicons names: https://developer.wordpress.org/resource/dashicons/

0

#6 How to write a plugin/theme with multiple blocks

Creating the src folder

  1. Run the command:

    npx @wordpress/create-block@latest my-blocks --variant=dynamic
    cd my-blocks

    For more details, see the manual https://developer.wordpress.org/block-editor/getting-started/tutorial/

  2. Move the contents of the src directory to a subdirectory, for example block-a: src/block-a.

  3. Duplicate the block-a subdirectory to create a second block, and name it, for example, block-b.

  4. Update the block.json files in each subdirectory to meet the block requirements.

    The structure should look like this:

    my-blocks
    ├── package.json
    ├── package-lock.json
    └── src
    	├── block-a
    	│   ├── block.json
    	│   ├── edit.js
    	│   ├── editor.scss
    	│   ├── index.js
    	│   ├── render.php
    	│   ├── style.scss
    	│   └── view.js
    	└── block-b
    		├── block.json
    		├── edit.js
    		├── editor.scss
    		├── index.js
    		├── render.php
    		├── style.scss
    		└── view.js

    Example content of block.json:

    {
    	"$schema": "https://schemas.wp.org/trunk/block.json",
    	"apiVersion": 3,
    	"name": "create-block/block-a",
    	"version": "0.1.0",
    	"title": "Block A",
    	"category": "widgets",
    	"icon": "smiley",
    	"description": "Example block scaffolded with Create Block tool.",
    	"example": {},
    	"supports": {
    		"html": false
    	},
    	"textdomain": "wpkama",
    	"render": "file:./render.php",
    	"editorScript": "file:./index.js",
    	"editorStyle": "file:./index.css",
    	"viewStyle": "file:./style-index.css",
    	"viewScript": "file:./view.js"
    }
  5. Run the command npm run build in the my-blocks directory. Corresponding directories will be created in the my-blocks/build folder.

Registering blocks

Now you need to register the blocks in PHP, pointing to the corresponding directory in the build folder:

add_action( 'init', 'wpdocs_create_blocks_mysite_block_init' );

function wpdocs_create_blocks_mysite_block_init() {

	register_block_type( __DIR__ . '/build/block-a' );
	register_block_type( __DIR__ . '/build/block-b' );
}

Moving the blocks folder inside the project

If your npm packages folder node_modules is located somewhere above, and the blocks should be inside, for example in the theme folder, you can specify the paths where the sources are located and where to output the builds.

To do this, add options to the build and start scripts in the package.json file:

"scripts": {
	"build": "wp-scripts build --webpack-src-dir=path/to/my-blocks/src/ --output-path=path/to/my-blocks/build/ --webpack-copy-php",
	"start": "wp-scripts start --webpack-src-dir=path/to/my-blocks/src/ --output-path=path/to/my-blocks/build/ --webpack-copy-php",
	...
}

Now npm run build can be run from the folder where package.json is located, and the blocks will be built in the internal folder (where you specified).

Changelog

Since 5.0.0 Introduced.
Since 5.8.0 First parameter now accepts a path to the block.json file.

register_block_type() code WP 6.8.3

function register_block_type( $block_type, $args = array() ) {
	if ( is_string( $block_type ) && file_exists( $block_type ) ) {
		return register_block_type_from_metadata( $block_type, $args );
	}

	return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
}