WP_REST_Templates_Controller::delete_item()publicWP 5.8.0

Deletes a single template.

{} It's a method of the class: WP_REST_Templates_Controller{}

No Hooks.

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->delete_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.8.0 Introduced.

Code of WP_REST_Templates_Controller::delete_item() WP 6.0

public function delete_item( $request ) {
	$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 ) );
	}
	if ( 'custom' !== $template->source ) {
		return new WP_Error( 'rest_invalid_template', __( 'Templates based on theme files can\'t be removed.' ), array( 'status' => 400 ) );
	}

	$id    = $template->wp_id;
	$force = (bool) $request['force'];

	$request->set_param( 'context', 'edit' );

	// If we're forcing, then delete permanently.
	if ( $force ) {
		$previous = $this->prepare_item_for_response( $template, $request );
		$result   = wp_delete_post( $id, true );
		$response = new WP_REST_Response();
		$response->set_data(
			array(
				'deleted'  => true,
				'previous' => $previous->get_data(),
			)
		);
	} else {
		// Otherwise, only trash if we haven't already.
		if ( 'trash' === $template->status ) {
			return new WP_Error(
				'rest_template_already_trashed',
				__( 'The template has already been deleted.' ),
				array( 'status' => 410 )
			);
		}

		// (Note that internally this falls through to `wp_delete_post()`
		// if the Trash is disabled.)
		$result           = wp_trash_post( $id );
		$template->status = 'trash';
		$response         = $this->prepare_item_for_response( $template, $request );
	}

	if ( ! $result ) {
		return new WP_Error(
			'rest_cannot_delete',
			__( 'The template cannot be deleted.' ),
			array( 'status' => 500 )
		);
	}

	return $response;
}