WP_Block_Type_Registry
- Since
- 5.0.0
- Source
wp-includes/class-wp-block-type-registry.php:15
Core class used for interacting with block types.
Compatibility
- WordPress
- since 5.0.0
- 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).
Properties · 2
$registered_block_typesWP_Block_Type[]private- Registered block types, as
$name => $instancepairs. $instanceWP_Block_Type_Registry|nullprivate static- Container for the main instance of the class.
Methods · 7
- register()Registers a block type.
- unregister()Unregisters a block type.
- get_registered()Retrieves a registered block type.
- get_all_registered()Retrieves all registered block types.
- is_registered()Checks if a block type is registered.
- __wakeup()
- get_instance()Utility method to retrieve the main instance of the class.
Source code
#[AllowDynamicProperties]final class WP_Block_Type_Registry { /** * Registered block types, as `$name => $instance` pairs. * * @since 5.0.0 * @var WP_Block_Type[] */ private $registered_block_types = array(); /** * Container for the main instance of the class. * * @since 5.0.0 * @var WP_Block_Type_Registry|null */ private static $instance = null; /** * Registers a block type. * * @since 5.0.0 * * @see WP_Block_Type::__construct() * * @param string|WP_Block_Type $name 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. * @param array $args 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 empty array. * @return WP_Block_Type|false The registered block type on success, or false on failure. */ 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; }Changelog
Introduced in 5.0.0. Unchanged from 6.7.7 through 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.