WP_Block_Templates_Registry
- Since
- 6.7.0
- Source
wp-includes/class-wp-block-templates-registry.php:14
Core class used for interacting with templates.
Compatibility
- WordPress
- since 6.7.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_templatesWP_Block_Template[]private- Registered templates, as
$name => $instancepairs. $instanceWP_Block_Templates_Registry|nullprivate static- Container for the main instance of the class.
Methods · 8
- register()Registers a template.
- get_all_registered()Retrieves all registered templates.
- get_registered()Retrieves a registered template by its name.
- get_by_slug()Retrieves a registered template by its slug.
- get_by_query()Retrieves registered templates matching a query.
- is_registered()Checks if a template is registered.
- unregister()Unregisters a template.
- get_instance()Utility method to retrieve the main instance of the class.
Source code
final class WP_Block_Templates_Registry { /** * Registered templates, as `$name => $instance` pairs. * * @since 6.7.0 * @var WP_Block_Template[] $registered_block_templates Registered templates. */ private $registered_templates = array(); /** * Container for the main instance of the class. * * @since 6.7.0 * @var WP_Block_Templates_Registry|null */ private static $instance = null; /** * Registers a template. * * @since 6.7.0 * * @param string $template_name Template name including namespace. * @param array $args Optional. Array of template arguments. * @return WP_Block_Template|WP_Error The registered template on success, or WP_Error on failure. */ public function register( $template_name, $args = array() ) { $template = null; $error_message = ''; $error_code = ''; if ( ! is_string( $template_name ) ) { $error_message = __( 'Template names must be strings.' ); $error_code = 'template_name_no_string'; } elseif ( preg_match( '/[A-Z]+/', $template_name ) ) { $error_message = __( 'Template names must not contain uppercase characters.' ); $error_code = 'template_name_no_uppercase'; } elseif ( ! preg_match( '/^[a-z0-9_\-]+\/\/[a-z0-9_\-]+$/', $template_name ) ) { $error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template' ); $error_code = 'template_no_prefix'; } elseif ( $this->is_registered( $template_name ) ) { /* translators: %s: Template name. */ $error_message = sprintf( __( 'Template "%s" is already registered.' ), $template_name ); $error_code = 'template_already_registered'; } if ( $error_message ) { _doing_it_wrong( __METHOD__, $error_message, '6.7.0' ); return new WP_Error( $error_code, $error_message ); } if ( ! $template ) { $theme_name = get_stylesheet(); list( $plugin, $slug ) = explode( '//', $template_name ); $default_template_types = get_default_block_template_types(); $template = new WP_Block_Template(); $template->id = $theme_name . '//' . $slug; $template->theme = $theme_name; $template->plugin = $plugin; $template->author = null; $template->content = $args['content'] ?? ''; $template->source = 'plugin'; $template->slug = $slug; $template->type = 'wp_template'; $template->title = $args['title'] ?? $template_name; $template->description = $args['description'] ?? ''; $template->status = 'publish'; $template->origin = 'plugin'; $template->is_custom = ! isset( $default_template_types[ $template_name ] ); $template->post_types = $args['post_types'] ?? array(); } $this->registered_templates[ $template_name ] = $template; return $template; }Changelog
Introduced in 6.7.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-templates-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.