wppaste
WordPress

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

Since
5.0.0
Source
wp-includes/class-wp-block-type-registry.php:48
Registers a block type.

Compatibility

WordPress
since 5.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$namestring|WP_Block_Type
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.
$argsarrayoptional
Array of block type arguments. Accepts any public property of WP_Block_Type. See WP_Block_Type::__construct() for information on accepted arguments. Default empty array.Default: array()

Return value

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

Performance profile

How much work a call to WP_Block_Type_Registry::register() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
20–31

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Block_Type_Registry::register()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block_Type_Registry::register() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_string($name) && !->is_registered()20–28->is_registered()
!is_string($name)21–24__(), sprintf(), _doing_it_wrong()
is_string($name)25–30__(), esc_html(), sprintf(), _doing_it_wrong()
is_string($name) && ->is_registered()28–31->is_registered(), __(), sprintf(), _doing_it_wrong()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8720–316
8.58720–316
8.48720–3166 fewer instructions than PHP 8.3
8.39321–376
8.29321–376
8.19321–376
7.49321–376

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

Source code

	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__,				/* translators: %s: The received block type name type. */				sprintf( __( 'Block type names must be strings, received %s.' ), gettype( $name ) ),				'5.0.0'			);			return false;		} 		if ( preg_match( '/[A-Z]+/', $name ) ) {			_doing_it_wrong(				__METHOD__,				/* translators: %s: Block name. */				sprintf( __( 'Block type names must not contain uppercase characters. "%s" was given.' ), esc_html( $name ) ),				'5.0.0'			);			return false;		} 		$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';		if ( ! preg_match( $name_matcher, $name ) ) {			_doing_it_wrong(				__METHOD__,				/* translators: %s: Block name. */				sprintf( __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type. "%s" was given.' ), esc_html( $name ) ),				'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;	}

Changelog

Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-block-type-registry.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.