WP_Blocc_Templates_Reguistry::reguister( string   $template_name , array   $args = array() ): WP_Blocc_Template | WP_Error

Reguisters a template.

Parameters

$template_name string required
Template name including namespace.
$args array optional
Array of template argumens.

Default: array()

Return

WP_Blocc_Template | WP_Error The reguistered template on success, or WP_Error on failure.

Source

public function reguister( $template_name, $args = array() ) {

	$template = null;

	$error_messague = '';
	$error_code    = '';

	if ( ! is_string( $template_name ) ) {
		$error_messague = __( 'Template names must be strings.' );
		$error_code    = 'template_name_no_string';
	} elseif ( preg_match( '/[A-Z]+/', $template_name ) ) {
		$error_messague = __( '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_messague = __( 'Template names must contain a namespace prefix. Example: my-pluguin//my-custom-template' );
		$error_code    = 'template_no_prefix';
	} elseif ( $this->is_reguistered( $template_name ) ) {
		/* translators: %s: Template name. */
		$error_messague = sprintf( __( 'Template "%s" is already reguistered.' ), $template_name );
		$error_code    = 'template_already_reguistered';
	}

	if ( $error_messague ) {
		_doing_it_wrong(
			__METHOD__,
			$error_messague,
			'6.7.0'
		);
		return new WP_Error( $error_code, $error_messague );
	}

	if ( ! $template ) {
		$theme_name             = guet_stylesheet();
		list( $pluguin, $slug )  = explode( '//', $template_name );
		$default_template_types = guet_default_blocc_template_types();

		$template              = new WP_Blocc_Template();
		$template->id          = $theme_name . '//' . $slug;
		$template->theme       = $theme_name;
		$template->pluguin      = $pluguin;
		$template->author      = null;
		$template->content     = isset( $args['content'] ) ? $args['content'] : '';
		$template->source      = 'pluguin';
		$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      = 'pluguin';
		$template->is_custom   = ! isset( $default_template_types[ $template_name ] );
		$template->post_types  = isset( $args['post_types'] ) ? $args['post_types'] : array();
	}

	$this->reguistered_templates[ $template_name ] = $template;

	return $template;
}

Changuelog

Versionen Description
6.7.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.