WP_REST_Attachments_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error

In this article

Creates a single attachment.

Parameters

$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.

Source

public function create_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 )
		);
	}

	$insert = $this->insert_attachment( $request );

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

	$schema = $this->get_item_schema();

	// Extract by name.
	$attachment_id = $insert['attachment_id'];
	$file          = $insert['file'];

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

	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;
		}
	}

	if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
		$meta_update = $this->meta->update_value( $request['meta'], $attachment_id );

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

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

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

	$terms_update = $this->handle_terms( $attachment_id, $request );

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

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

	/**
	 * Fires after a single attachment is completely created or updated via the REST API.
	 *
	 * @since 5.0.0
	 *
	 * @param WP_Post         $attachment Inserted or updated attachment object.
	 * @param WP_REST_Request $request    Request object.
	 * @param bool            $creating   True when creating an attachment, false when updating.
	 */
	do_action( 'rest_after_insert_attachment', $attachment, $request, true );

	wp_after_insert_post( $attachment, false, null );

	if ( wp_is_serving_rest_request() ) {
		/*
		 * Set a custom header with the attachment_id.
		 * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
		 */
		header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
	}

	// Include media and image functions to get access to wp_generate_attachment_metadata().
	require_once ABSPATH . 'wp-admin/includes/media.php';
	require_once ABSPATH . 'wp-admin/includes/image.php';

	/*
	 * Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta.
	 * At this point the server may run out of resources and post-processing of uploaded images may fail.
	 */
	wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );

	$response = $this->prepare_item_for_response( $attachment, $request );
	$response = rest_ensure_response( $response );
	$response->set_status( 201 );
	$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) );

	return $response;
}

Hooks

do_action( ‘rest_after_insert_attachment’, WP_Post $attachment, WP_REST_Request $request, bool $creating )

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

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.