WP_REST_Templates_Controller::create_item( WP_REST_Request $request )

Creates a single template.

Parameters

$request

(WP_REST_Request) (Required) Full details about the request.

Return

(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

public function create_item( $request ) {
		$changes            = $this->prepare_item_for_database( $request );
		$changes->post_name = $request['slug'];
		$result             = wp_insert_post( wp_slash( (array) $changes ), true );
		if ( is_wp_error( $result ) ) {
			return $result;
		}
		$posts = get_block_templates( array( 'wp_id' => $result ), $this->post_type );
		if ( ! count( $posts ) ) {
			return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ) );
		}
		$id            = $posts[0]->id;
		$template      = get_block_template( $id, $this->post_type );
		$fields_update = $this->update_additional_fields_for_object( $template, $request );
		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		return $this->prepare_item_for_response(
			get_block_template( $id, $this->post_type ),
			$request
		);
	}

Changelog

Version Description
5.8.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_templates_controller/create_item