# wp_ajax_media_create_image_subsizes()

URL: https://chugunov.pro/api-wordpress/functions/wp_ajax_media_create_image_subsizes/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 5.3.0.

## Сигнатура

```php
wp_ajax_media_create_image_subsizes()
```

## Описание

Обрабатывает создание недостающих подразмеров изображения для только что загруженных изображений через AJAX.

## Исходный код

Файл: `wp-admin/includes/ajax-actions.php:2514`

```php
function wp_ajax_media_create_image_subsizes() {
	check_ajax_referer( 'media-form' );

	if ( ! current_user_can( 'upload_files' ) ) {
		wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) );
	}

	if ( empty( $_POST['attachment_id'] ) ) {
		wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
	}

	$attachment_id = (int) $_POST['attachment_id'];

	if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) {
		// Upload failed. Cleanup.
		if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) {
			$attachment = get_post( $attachment_id );

			// Created at most 10 min ago.
			if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) {
				wp_delete_attachment( $attachment_id, true );
				wp_send_json_success();
			}
		}
	}

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

	/*
	 * This can still be pretty slow and cause timeout or out of memory errors.
	 * The js that handles the response would need to also handle HTTP 500 errors.
	 */
	wp_update_image_subsizes( $attachment_id );

	if ( ! empty( $_POST['_legacy_support'] ) ) {
		// The old (inline) uploader. Only needs the attachment_id.
		$response = array( 'id' => $attachment_id );
	} else {
		// Media modal and Media Library grid view.
		$response = wp_prepare_attachment_for_js( $attachment_id );

		if ( ! $response ) {
			wp_send_json_error( array( 'message' => __( 'Upload failed.' ) ) );
		}
	}

	// At this point the image has been uploaded successfully.
	wp_send_json_success( $response );
}
```

## История изменений

- 5.3.0 — Introduced.

## Связанные

Использует: [`wp_update_image_subsizes`](https://chugunov.pro/api-wordpress/functions/wp_update_image_subsizes/), [`wp_prepare_attachment_for_js`](https://chugunov.pro/api-wordpress/functions/wp_prepare_attachment_for_js/), [`wp_attachment_is_image`](https://chugunov.pro/api-wordpress/functions/wp_attachment_is_image/), [`wp_delete_attachment`](https://chugunov.pro/api-wordpress/functions/wp_delete_attachment/), [`current_user_can`](https://chugunov.pro/api-wordpress/functions/current_user_can/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`check_ajax_referer`](https://chugunov.pro/api-wordpress/functions/check_ajax_referer/), [`wp_send_json_error`](https://chugunov.pro/api-wordpress/functions/wp_send_json_error/), [`wp_send_json_success`](https://chugunov.pro/api-wordpress/functions/wp_send_json_success/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_ajax_media_create_image_subsizes/
