wppaste
WordPress

WP_Block_Templates_Registry::register( string $template_name, array $args = array() ): WP_Block_Template|WP_Error

Since
6.7.0
Source
wp-includes/class-wp-block-templates-registry.php:40
Registers a template.

Compatibility

WordPress
since 6.7.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

$template_namestring
Template name including namespace.
$argsarrayoptional
Array of template arguments.Default: array()

Return value

WP_Block_Template|WP_Error
The registered template on success, or WP_Error on failure.

Performance profile

How much work a call to WP_Block_Templates_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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
23–88

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

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_Templates_Registry::register()
  • optionoption read or writeget_option()one call below WP_Block_Templates_Registry::register()

Further down the call graph this can also reach 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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
always23–27__(), _doing_it_wrong()
is_string($template_name) && !->is_registered()25->is_registered(), _doing_it_wrong()
is_string($template_name) && ->is_registered()34->is_registered(), __(), sprintf(), _doing_it_wrong()
always73–81__(), get_stylesheet(), explode(), get_default_block_template_types()
is_string($template_name) && !->is_registered()75–79->is_registered(), get_stylesheet(), explode(), get_default_block_template_types()
is_string($template_name) && ->is_registered()84–88->is_registered(), __(), sprintf(), get_stylesheet(), explode(), get_default_block_template_types()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11623–889
8.511623–889
8.411623–8896 fewer instructions than PHP 8.3
8.312223–949
8.212223–949
8.112223–949
7.412223–949

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

Source code

	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.

  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-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.