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.
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()?
- Why is a block I just registered missing from the registry?
- How do I reset the registry back to empty for a test?
Why does instantiating WP_Block_Type_Registry directly with new not behave like get_instance()?
Why is a block I just registered missing from the registry?
How do I reset the registry back to empty for a test?
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
- Scaling
- Constant
- Instructions
- 5–9
- Plugin surface
- None
- Called by
- 38
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 9.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–9 | none |
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
- WP_Block_List::__construct()Constructor.
- WP_Block_Supports::apply_block_supports()Generates an array of HTML attributes, such as classes, by applying to the given block all of the features that the block supports.
- WP_Block_Supports::register_attributes()Registers the block attributes required by the different block supports.
- WP_Navigation_Fallback::get_default_fallback_blocks()Gets the rendered markup for the default fallback blocks.
- WP_REST_Block_Renderer_Controller::get_item()Returns block output from block's registered render_callback.
- WP_REST_Block_Renderer_Controller::register_routes()Registers the necessary REST API routes, one for each dynamic block.
- WP_REST_Block_Types_Controller::__construct()Constructor.
- WP_REST_Widget_Types_Controller::render_legacy_widget_preview_iframe()Renders a page containing a preview of the requested Legacy Widget block.
- WP_Theme_JSON::get_blocks_metadata()Returns the metadata for each block.
- WP_Theme_JSON::get_layout_styles()Gets the CSS layout rules for a particular block from theme.json layout definitions.
- WP_Theme_JSON_Resolver::get_block_data()Gets the styles for blocks from the block.json file.
- WP_Theme_JSON_Resolver::has_same_registered_blocks()Checks whether the registered blocks were already processed for this origin.
Show all 38
- _wp_add_block_level_preset_styles()Render the block level presets stylesheet.
- _wp_add_block_level_presets_class()Update the block content with block level presets class name.
- _wp_enqueue_auto_register_blocks()Exposes blocks with autoRegister flag for ServerSideRender in the editor.
- _wp_get_iframed_editor_assets()Collect the block editor assets that need to be loaded into the editor's iframe.
- apply_block_hooks_to_content()Runs the hooked blocks algorithm on the given content.
- block_core_navigation_get_fallback_blocks()Retrieves the appropriate fallback to be used on the front of the site when there is no menu assigned to the Nav block.
- block_core_query_disable_enhanced_pagination()Traverse the tree of blocks looking for any plugin block (i.e., a block from an installed plugin) inside a Query block with the enhanced pagination enabled. If at least one is found, the enhanced pagination is effectively disabled to prevent any potential incompatibilities.
- get_block_editor_server_block_settings()Prepares server-registered blocks for the block editor.
- get_block_editor_settings()Returns the contextualized block editor settings for a selected editor context.
- get_dynamic_block_names()Returns an array of the names of all registered dynamic block types.
- get_hooked_blocks()Retrieves block types hooked into the given block, grouped by anchor block type and the relative position.
- handle_legacy_widget_preview_iframe()Intercepts any request with legacy-widget-preview in the query param and, if set, renders a page containing a preview of the requested Legacy Widget block.
- register_block_type()Registers a block type. The recommended way is to register a block type using the metadata stored in the `block.json` file.
- register_block_type_from_metadata()Registers a block type from the metadata stored in the `block.json` file.
- register_legacy_post_comments_block()Ensures backwards compatibility for any users running the Gutenberg plugin who have used Post Comments before it was merged into Comments Query Loop.
- unregister_block_type()Unregisters a block type.
- wp_enqueue_registered_block_scripts_and_styles()Enqueues registered block scripts and styles, depending on current rendered context (only enqueuing editor scripts while in context of the editor).
- wp_hoist_late_printed_styles()Adds the hooks needed for CSS output to be delayed until after the content of the page has been established.
- wp_render_background_support()Renders the background styles to the block wrapper.
- wp_render_block_states_support()Renders per-instance state styles on the frontend.
- wp_render_block_visibility_support()Render nothing if the block is hidden, or add viewport visibility styles.
- wp_render_custom_css_support_styles()Render the custom CSS stylesheet and add class name to block as required.
- wp_render_dimensions_support()Renders server-side dimensions styles to the block wrapper.
- wp_render_elements_support_styles()Render the elements stylesheet and adds elements class name to block as required.
- wp_render_layout_support_flag()Renders the layout config to the block wrapper.
- wp_render_position_support()Renders position styles to the block wrapper.
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.
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.