On this page
WP_REST_Templates_Controller::update_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
Updates a single template.
Parameters
$requestWP_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. View all references
public function update_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 ) );
}
$post_before = get_post( $template->wp_id );
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
wp_delete_post( $template->wp_id, true );
$request->set_param( 'context', 'edit' );
$template = get_block_template( $request['id'], $this->post_type );
$response = $this->prepare_item_for_response( $template, $request );
return rest_ensure_response( $response );
}
$changes = $this->prepare_item_for_database( $request );
if ( is_wp_error( $changes ) ) {
return $changes;
}
if ( 'custom' === $template->source ) {
$update = true;
$result = wp_update_post( wp_slash( (array) $changes ), false );
} else {
$update = false;
$post_before = null;
$result = wp_insert_post( wp_slash( (array) $changes ), false );
}
if ( is_wp_error( $result ) ) {
if ( 'db_update_error' === $result->get_error_code() ) {
$result->add_data( array( 'status' => 500 ) );
} else {
$result->add_data( array( 'status' => 400 ) );
}
return $result;
}
$template = get_block_template( $request['id'], $this->post_type );
$fields_update = $this->update_additional_fields_for_object( $template, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$request->set_param( 'context', 'edit' );
$post = get_post( $template->wp_id );
/** 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, false );
wp_after_insert_post( $post, $update, $post_before );
$response = $this->prepare_item_for_response( $template, $request );
return rest_ensure_response( $response );
}
Hooks
- do_action( "rest_after_insert_{$this->post_type}",
WP_Post $post ,WP_REST_Request $request ,bool $creating ) -
Fires after a single post is completely created or updated via the REST API.
Related
Uses
| Uses | Description |
|---|---|
| WP_REST_Templates_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php | Prepare a single template output for response |
| WP_REST_Templates_Controller::prepare_item_for_database() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php | Prepares a single template for create or update. |
| get_block_template() wp-includes/block-template-utils.php | Retrieves a single unified template object using its id. |
| wp_after_insert_post() wp-includes/post.php | Fires actions after a post, its terms and meta data has been saved. |
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| wp_delete_post() wp-includes/post.php | Trashes or deletes a post or page. |
| rest_ensure_response() wp-includes/rest-api.php | Ensures a REST response is a response object (for consistency). |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_slash() wp-includes/formatting.php | Adds slashes to a string or recursively adds slashes to strings within an array. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_templates_controller/update_item