Title: WP_REST_Attachments_Controller::update_item
Published: December 6, 2016
Last modified: August 20, 2026

---

# WP_REST_Attachments_Controller::update_item( WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#changelog)

Updates a single attachment.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Full details about the request.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Response
object on success, [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#source)󠁿

    ```php
    public function update_item( $request ) {
    	if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
    		return new WP_Error(
    			'rest_invalid_param',
    			__( 'Invalid parent type.' ),
    			array( 'status' => 400 )
    		);
    	}

    	$attachment_before = get_post( $request['id'] );
    	$response          = parent::update_item( $request );

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

    	$response = rest_ensure_response( $response );
    	$data     = $response->get_data();

    	if ( isset( $request['alt_text'] ) ) {
    		update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] );
    	}

    	$attachment = get_post( $request['id'] );

    	if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
    		$thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID );

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

    	$fields_update = $this->update_additional_fields_for_object( $attachment, $request );

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

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

    	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
    	do_action( 'rest_after_insert_attachment', $attachment, $request, false );

    	wp_after_insert_post( $attachment, true, $attachment_before );

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

    	return $response;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L971)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L971-L1021)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#hooks)󠁿

 [do_action( ‘rest_after_insert_attachment’, WP_Post $attachment, WP_REST_Request $request, bool $creating )](https://developer.wordpress.org/reference/hooks/rest_after_insert_attachment/)

Fires after a single attachment is completely created or updated via the REST API.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_Attachments_Controller::handle_featured_media()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/handle_featured_media/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Determines the featured media based on a request param.

  | 
| [wp_after_insert_post()](https://developer.wordpress.org/reference/functions/wp_after_insert_post/)`wp-includes/post.php` |

Fires actions after a post, its terms and meta data has been saved.

  | 
| [WP_REST_Attachments_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment output for response.

  | 
| [WP_REST_Posts_Controller::update_item()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/update_item/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Updates a single post.

  | 
| [update_post_meta()](https://developer.wordpress.org/reference/functions/update_post_meta/)`wp-includes/post.php` |

Updates a post meta field based on the given post ID.

  | 
| [get_post_type()](https://developer.wordpress.org/reference/functions/get_post_type/)`wp-includes/post.php` |

Retrieves the post type of the current post or of a given post.

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 7 more](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/update_item/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_attachments_controller%2Fupdate_item%2F)
before being able to contribute a note or feedback.