wppaste
WordPress

WP_Block_Type_Registry::get_instance(): WP_Block_Type_Registry

Since
5.0.0
Source
wp-includes/class-wp-block-type-registry.php:197

Returns WordPress's single, site-wide WP_Block_Type_Registry object, creating it the first time it is asked for. Every block registration and every block-rendering call in core goes through this same shared instance, so code that reads it always sees whatever blocks have been registered so far in the current request. It pairs with register_block_type() to add blocks and with instance methods like is_registered() or get_all_registered() to inspect them.

Utility method to retrieve the main instance of the class.

Description

The instance will be created if it does not exist yet.

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.

Return value

WP_Block_Type_Registry
The main instance.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check whether a specific block type is registered

A fresh WordPress install already registers all the core blocks, so this runs against them directly.

$registry = WP_Block_Type_Registry::get_instance();

if ( $registry->is_registered( 'core/paragraph' ) ) {
	echo esc_html( 'core/paragraph is registered.' );
} else {
	echo esc_html( 'core/paragraph is not registered.' );
}

Count how many blocks each namespace contributes to the registry

Pull every registered block from the singleton and group the names by their namespace prefix.

$registry = WP_Block_Type_Registry::get_instance();
$blocks   = $registry->get_all_registered();

$counts = array();
foreach ( $blocks as $name => $block_type ) {
	$namespace = strtok( $name, '/' );
	if ( ! isset( $counts[ $namespace ] ) ) {
		$counts[ $namespace ] = 0;
	}
	$counts[ $namespace ]++;
}

print_r( $counts );

On a stock install every key here will be core, but any plugin that registers its own blocks will add its own namespace to the list.

Common problems and fixes · 3

Why does instantiating WP_Block_Type_Registry directly with new not behave like get_instance()?

get_instance() is the only place core ever creates the registry with new self(), and it stores that one object in a static property for the whole request. A separate object you create yourself starts with none of the blocks WordPress has already registered.

Why is a block I just registered missing from the registry?

This is purely a timing issue: register_block_type() has to run before you read the registry, and block registration normally happens on the init hook. Calling get_instance() and checking is_registered() earlier in the request will see a registry that is not yet fully populated.

How do I reset the registry back to empty for a test?

There is no reset built into get_instance(); the static property it returns is cached for the life of the request and the method has no way to clear it.

Alternatives and related functions

register_block_type
When you want to add a block type to the registry rather than read from the one that already exists.
unregister_block_type
When you need to remove a block type that something else registered, instead of trying to reset the whole registry.
WP_Block_Type_Registry::get_all_registered
When you already have the instance and want the full list of block types rather than the singleton itself.

Performance profile

How much work a call to WP_Block_Type_Registry::get_instance() 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
5–9

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
38

38 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always5–9none

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 9 instructions, 5–9 executed per call, 1 branch. The work does not change between versions.

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 · 1

  • WP_Block_Type_Registry::__construct()

Used by · 38

Show all 38

Source code

	public static function get_instance() {		if ( null === self::$instance ) {			self::$instance = new self();		} 		return self::$instance;	}

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.