WP_Block_Type_Registry::register() WordPress Method

The WP_Block_Type_Registry::register() method is used to register a new block type. This method accepts two arguments: 1. $block_type: An instance of the WP_Block_Type class. 2. $args: An array of arguments used to configure the new block type.

WP_Block_Type_Registry::register( string|WP_Block_Type $name, array $args = array() ) #

Registers a block type.


Description

Top ↑

See also


Top ↑

Parameters

$name

(string|WP_Block_Type)(Required)Block type name including namespace, or alternatively a complete WP_Block_Type instance. In case 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.

Default value: array()


Top ↑

Return

(WP_Block_Type|false) The registered block type on success, or false on failure.


Top ↑

Source

File: wp-includes/class-wp-block-type-registry.php

	public function register( $name, $args = array() ) {
		$block_type = null;
		if ( $name instanceof WP_Block_Type ) {
			$block_type = $name;
			$name       = $block_type->name;
		}

		if ( ! is_string( $name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Block type names must be strings.' ),
				'5.0.0'
			);
			return false;
		}

		if ( preg_match( '/[A-Z]+/', $name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Block type names must not contain uppercase characters.' ),
				'5.0.0'
			);
			return false;
		}

		$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
		if ( ! preg_match( $name_matcher, $name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' ),
				'5.0.0'
			);
			return false;
		}

		if ( $this->is_registered( $name ) ) {
			_doing_it_wrong(
				__METHOD__,
				/* translators: %s: Block name. */
				sprintf( __( 'Block type "%s" is already registered.' ), $name ),
				'5.0.0'
			);
			return false;
		}

		if ( ! $block_type ) {
			$block_type = new WP_Block_Type( $name, $args );
		}

		$this->registered_block_types[ $name ] = $block_type;

		return $block_type;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.