WP_REST_Templates_Controller::get_item() WordPress Method

The WP_REST_Templates_Controller::get_item() method is used to retrieve a single template from a WordPress site. This is useful for retrieving a specific template for use in a custom plugin or theme.

WP_REST_Templates_Controller::get_item( WP_REST_Request $request ) #

Returns the given template


Parameters

$request

(WP_REST_Request)(Required)The request instance.


Top ↑

Return

(WP_REST_Response|WP_Error)


Top ↑

Source

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

	public function get_item( $request ) {
		if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
			$template = get_block_file_template( $request['id'], $this->post_type );
		} else {
			$template = get_block_template( $request['id'], $this->post_type );
		}

		if ( ! $template ) {
			return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
		}

		return $this->prepare_item_for_response( $template, $request );
	}


Top ↑

Changelog

Changelog
VersionDescription
5.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.