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

---

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

## In this article

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

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_database/?output_format=md#wp--skip-link--target)

Prepares a single attachment for create or update.

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

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

Request object.

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

 stdClass|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
Post object.

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

    ```php
    protected function prepare_item_for_database( $request ) {
    	$prepared_attachment = parent::prepare_item_for_database( $request );

    	// Attachment caption (post_excerpt internally).
    	if ( isset( $request['caption'] ) ) {
    		if ( is_string( $request['caption'] ) ) {
    			$prepared_attachment->post_excerpt = $request['caption'];
    		} elseif ( isset( $request['caption']['raw'] ) ) {
    			$prepared_attachment->post_excerpt = $request['caption']['raw'];
    		}
    	}

    	// Attachment description (post_content internally).
    	if ( isset( $request['description'] ) ) {
    		if ( is_string( $request['description'] ) ) {
    			$prepared_attachment->post_content = $request['description'];
    		} elseif ( isset( $request['description']['raw'] ) ) {
    			$prepared_attachment->post_content = $request['description']['raw'];
    		}
    	}

    	if ( isset( $request['post'] ) ) {
    		$prepared_attachment->post_parent = (int) $request['post'];
    	}

    	return $prepared_attachment;
    }
    ```

[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#L1387)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L1387-L1413)

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

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

Prepares a single post for create or update.

  |

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

Applies edits to a media item and creates a new attachment record.

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

Inserts the attachment post in the database. Does not update the attachment meta.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_database/?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%2Fprepare_item_for_database%2F)
before being able to contribute a note or feedback.