WP_REST_Attachments_Controller::insert_attachment( WP_REST_Request $request ): array|WP_Error

In this article

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

Parameters

$requestWP_REST_Requestrequired

Return

array|WP_Error

Source

protected function insert_attachment( $request ) {
	// Get the file via $_FILES or raw data.
	$files   = $request->get_file_params();
	$headers = $request->get_headers();

	$time = null;

	// Matches logic in media_handle_upload().
	if ( ! empty( $request['post'] ) ) {
		$post = get_post( $request['post'] );
		// The post date doesn't usually matter for pages, so don't backdate this upload.
		if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
			$time = $post->post_date;
		}
	}

	if ( ! empty( $files ) ) {
		$file = $this->upload_from_file( $files, $headers, $time );
	} else {
		$file = $this->upload_from_data( $request->get_body(), $headers, $time );
	}

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

	$name       = wp_basename( $file['file'] );
	$name_parts = pathinfo( $name );
	$name       = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );

	$url  = $file['url'];
	$type = $file['type'];
	$file = $file['file'];

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

	// Use image exif/iptc data for title and caption defaults if possible.
	$image_meta = wp_read_image_metadata( $file );

	if ( ! empty( $image_meta ) ) {
		if ( empty( $request['title'] ) && trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
			$request['title'] = $image_meta['title'];
		}

		if ( empty( $request['caption'] ) && trim( $image_meta['caption'] ) ) {
			$request['caption'] = $image_meta['caption'];
		}
	}

	$attachment = $this->prepare_item_for_database( $request );

	$attachment->post_mime_type = $type;
	$attachment->guid           = $url;

	// If the title was not set, use the original filename.
	if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
		// Remove the file extension (after the last `.`)
		$tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );

		if ( ! empty( $tmp_title ) ) {
			$attachment->post_title = $tmp_title;
		}
	}

	// Fall back to the original approach.
	if ( empty( $attachment->post_title ) ) {
		$attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
	}

	// $post_parent is inherited from $attachment['post_parent'].
	$id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true, false );

	if ( is_wp_error( $id ) ) {
		if ( 'db_update_error' === $id->get_error_code() ) {
			$id->add_data( array( 'status' => 500 ) );
		} else {
			$id->add_data( array( 'status' => 400 ) );
		}

		return $id;
	}

	$attachment = get_post( $id );

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

	return array(
		'attachment_id' => $id,
		'file'          => $file,
	);
}

Hooks

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

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

Changelog

VersionDescription
5.3.0Introduced.

User Contributed Notes

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