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

---

# _wp_make_subsizes( array $new_sizes, string $file, array $image_meta, int $attachment_id ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#user-contributed-notes)

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

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Low-level function to create image sub-sizes.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#description)󠁿

Updates the image meta after each sub-size is created.
Errors are stored in the 
returned image metadata array.

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

 `$new_sizes`arrayrequired

Array defining what sizes to create.

`$file`stringrequired

Full path to the image file.

`$image_meta`arrayrequired

The attachment meta data array.

`$attachment_id`intrequired

Attachment ID to process.

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

 array The attachment meta data with updated `sizes` array. Includes an array of
errors encountered while resizing.

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

    ```php
    function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
    	if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
    		// Not an image attachment.
    		return array();
    	}

    	// Check if any of the new sizes already exist.
    	if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
    		foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
    			/*
    			 * Only checks "size name" so we don't override existing images even if the dimensions
    			 * don't match the currently defined size with the same name.
    			 * To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
    			 */
    			if ( array_key_exists( $size_name, $new_sizes ) ) {
    				unset( $new_sizes[ $size_name ] );
    			}
    		}
    	} else {
    		$image_meta['sizes'] = array();
    	}

    	if ( empty( $new_sizes ) ) {
    		// Nothing to do...
    		return $image_meta;
    	}

    	/*
    	 * Sort the image sub-sizes in order of priority when creating them.
    	 * This ensures there is an appropriate sub-size the user can access immediately
    	 * even when there was an error and not all sub-sizes were created.
    	 */
    	$priority = array(
    		'medium'       => null,
    		'large'        => null,
    		'thumbnail'    => null,
    		'medium_large' => null,
    	);

    	$new_sizes = array_filter( array_merge( $priority, $new_sizes ) );

    	$editor = wp_get_image_editor( $file );

    	if ( is_wp_error( $editor ) ) {
    		// The image cannot be edited.
    		return $image_meta;
    	}

    	// If stored EXIF data exists, rotate the source image before creating sub-sizes.
    	if ( ! empty( $image_meta['image_meta'] ) ) {
    		$rotated = $editor->maybe_exif_rotate();

    		if ( is_wp_error( $rotated ) ) {
    			// TODO: Log errors.
    		}
    	}

    	if ( method_exists( $editor, 'make_subsize' ) ) {
    		foreach ( $new_sizes as $new_size_name => $new_size_data ) {
    			$new_size_meta = $editor->make_subsize( $new_size_data );

    			if ( is_wp_error( $new_size_meta ) ) {
    				// TODO: Log errors.
    			} else {
    				// Save the size meta value.
    				$image_meta['sizes'][ $new_size_name ] = $new_size_meta;
    				wp_update_attachment_metadata( $attachment_id, $image_meta );
    			}
    		}
    	} else {
    		// Fall back to `$editor->multi_resize()`.
    		$created_sizes = $editor->multi_resize( $new_sizes );

    		if ( ! empty( $created_sizes ) ) {
    			$image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
    			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#L430)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/image.php#L430-L510)

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

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

Returns a [WP_Image_Editor](https://developer.wordpress.org/reference/classes/wp_image_editor/) instance and loads file into it.

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

Updates metadata for an attachment.

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

Checks whether the given variable is a WordPress Error.

  |

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

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

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

  | 
| [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_generate_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_generate_attachment_metadata/)`wp-admin/includes/image.php` |

Generates attachment meta data and create image sub-sizes for images.

  |

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/?output_format=md#comment-content-6180)
 2.   [filatovdanyl](https://profiles.wordpress.org/filatovdanyl/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/_wp_make_subsizes/#comment-6180)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_wp_make_subsizes%2F%23comment-6180)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_wp_make_subsizes%2F%23comment-6180)
 4. I know this is an internal wp function not meant to be used in theme or plugin 
    code, however using it is the only way I found to solve a problem: to programmatically
    create some additional image sizes when needed.
 5. For example I have a plugin that when we upload some image to it, it needs to have
    that image in 3 specific sizes. This plugin solves that by registering those sizes
    via add_image_size. But this way those sizes (and files) are created for all images
    uploaded to website, but only some of them will be used for that plugin. That is
    bad if multiple plugins do that and if you have over 25k uploads on a website –
    it reaches tens of gigs in disk usage with all those unnecessary thumbnails for
    every upload.
 6. So my solution to that is to remove_image_size for those plugin’s sizes, and to
    create needed sizes only for images actually used by that plugin based on some 
    hook like so:
 7.     ```php
        $attachment_id = 12345;
        $needed_sizes = array(
        	'some_size' => array(
        		'width' => 420,
        		'height' => 420,
        		'crop' => false
        	),
        	'some_size_2' => array(
        		'width' => 69,
        		'height' => 69,
        		'crop' => true
        	)
        );
        require_once ABSPATH . 'wp-admin/includes/image.php';
        _wp_make_subsizes( $needed_sizes, wp_get_original_image_path( $attachment_id ), wp_get_attachment_metadata( $attachment_id ), $attachment_id );
        ```
    
 8.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_wp_make_subsizes%2F%3Freplytocom%3D6180%23feedback-editor-6180)

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