Registers a template.
Parameters
$template_name
stringrequired- Template name including namespace.
$args
arrayoptional- Array of template arguments.
Default:
array()
Source
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 = isset( $args['content'] ) ? $args['content'] : '';
$template->source = 'plugin';
$template->slug = $slug;
$template->type = 'wp_template';
$template->title = isset( $args['title'] ) ? $args['title'] : $template_name;
$template->description = isset( $args['description'] ) ? $args['description'] : '';
$template->status = 'publish';
$template->origin = 'plugin';
$template->is_custom = ! isset( $default_template_types[ $template_name ] );
$template->post_types = isset( $args['post_types'] ) ? $args['post_types'] : array();
}
$this->registered_templates[ $template_name ] = $template;
return $template;
}
Changelog
Version | Description |
---|---|
6.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.