Title: wp_update_image_subsizes
Published: November 12, 2019
Last modified: February 24, 2026

---

# wp_update_image_subsizes( int $attachment_id ): array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

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

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

If any of the currently registered image sub-sizes are missing, create them and 
update the image meta data.

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

 `$attachment_id`intrequired

The image attachment post ID.

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

 array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) The
updated image meta data array or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object if both the image meta and the attached file are missing.

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

    ```php
    function wp_update_image_subsizes( $attachment_id ) {
    	$image_meta = wp_get_attachment_metadata( $attachment_id );
    	$image_file = wp_get_original_image_path( $attachment_id );

    	if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
    		/*
    		 * Previously failed upload?
    		 * If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
    		 */
    		if ( ! empty( $image_file ) ) {
    			$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
    		} else {
    			return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
    		}
    	} else {
    		$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );

    		if ( empty( $missing_sizes ) ) {
    			return $image_meta;
    		}

    		// This also updates the image meta.
    		$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
    	}

    	/** This filter is documented in wp-admin/includes/image.php */
    	$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );

    	// Save the updated metadata.
    	wp_update_attachment_metadata( $attachment_id, $image_meta );

    	return $image_meta;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/image.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/image.php#L159)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/image.php#L159-L191)

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

 [apply_filters( ‘wp_generate_attachment_metadata’, array $metadata, int $attachment_id, string $context )](https://developer.wordpress.org/reference/hooks/wp_generate_attachment_metadata/)

Filters the generated attachment meta data.

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

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

Retrieves the path to an uploaded image file.

  | 
| [wp_create_image_subsizes()](https://developer.wordpress.org/reference/functions/wp_create_image_subsizes/)`wp-admin/includes/image.php` |

Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.

  | 
| [wp_get_missing_image_subsizes()](https://developer.wordpress.org/reference/functions/wp_get_missing_image_subsizes/)`wp-admin/includes/image.php` |

Compare the existing image sub-sizes (as saved in the attachment meta) to the currently registered image sub-sizes, and return the difference.

  | 
| [_wp_make_subsizes()](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/)`wp-admin/includes/image.php` |

Low-level function to create image sub-sizes.

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

Retrieves attachment metadata for attachment ID.

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

Updates metadata for an attachment.

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

Retrieves the translation of $text.

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

Calls the callback functions that have been added to a filter hook.

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

Initializes the error.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/wp_update_image_subsizes/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_update_image_subsizes/?output_format=md#)

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

Performs post-processing on an attachment.

  | 
| [wp_ajax_media_create_image_subsizes()](https://developer.wordpress.org/reference/functions/wp_ajax_media_create_image_subsizes/)`wp-admin/includes/ajax-actions.php` |

Handles creating missing image sub-sizes for just uploaded images via AJAX.

  |

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

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

## User Contributed Notes

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