WP_REST_Templates_Controller::create_item()publicWP 5.8.0

Creates a single template.

Method of the class: WP_REST_Templates_Controller{}

Hooks from the method

Return

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

Usage

$WP_REST_Templates_Controller = new WP_REST_Templates_Controller();
$WP_REST_Templates_Controller->create_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.8.0 Introduced.

WP_REST_Templates_Controller::create_item() code WP 6.5.2

public function create_item( $request ) {
	$prepared_post = $this->prepare_item_for_database( $request );

	if ( is_wp_error( $prepared_post ) ) {
		return $prepared_post;
	}

	$prepared_post->post_name = $request['slug'];
	$post_id                  = wp_insert_post( wp_slash( (array) $prepared_post ), true );
	if ( is_wp_error( $post_id ) ) {
		if ( 'db_insert_error' === $post_id->get_error_code() ) {
			$post_id->add_data( array( 'status' => 500 ) );
		} else {
			$post_id->add_data( array( 'status' => 400 ) );
		}

		return $post_id;
	}
	$posts = get_block_templates( array( 'wp_id' => $post_id ), $this->post_type );
	if ( ! count( $posts ) ) {
		return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) );
	}
	$id            = $posts[0]->id;
	$post          = get_post( $post_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;
	}

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
	do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );

	wp_after_insert_post( $post, false, null );

	$response = $this->prepare_item_for_response( $template, $request );
	$response = rest_ensure_response( $response );

	$response->set_status( 201 );
	$response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $template->id ) ) );

	return $response;
}