Since version 2.9, WordPress provided a new function, add_image_size() to define additional image sizes along with the three default sizes: Thumbnail, Medium and Large. After defining the additional sizes, we can use them in our themes either using the_post_thumbnail(), wp_get_attachment_image(), or any other functions that suit your needs.
Update
I’ve created a plugin for this so you won’t need to edit your theme/plugin files anymore :) Download it from WordPress plugins repo.
Unfortunately, WordPress didn’t and still doesn’t provide us with the ability to insert images with these custom sizes into our posts. Currently, it only provides us with the three default sizes as shown in the figure below.
I thought it’d be cool if I could insert the images with custom sizes I defined, so I wanted to have the form modified like this (notice the additional vm-medium item):
The Solution
To achieve this, we’ll utilize the attachment_fields_to_edit hook to insert the custom image sizes.
function kc_get_additional_image_sizes() {
$sizes = array();
global $_wp_additional_image_sizes;
if ( isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes) ) {
$sizes = apply_filters( 'intermediate_image_sizes', $_wp_additional_image_sizes );
$sizes = apply_filters( 'kc_get_additional_image_sizes', $_wp_additional_image_sizes );
}
return $sizes;
}
kc_get_additional_image_sizes() is just a helper function to get custom image sizes. Actually, WordPress already has a function for this — get_intermediate_image_sizes() — but the array it returns also contains the default image sizes we don’t need. Sure we can remove those sizes after we got the array, but I thought I will need this helper function later in my plugins/themes, so I wrote it.
function kc_additional_image_size_input_fields( $fields, $post ) {
if ( !isset($fields['image-size']['html']) || substr($post->post_mime_type, 0, 5) != 'image' )
return $fields;
$sizes = kc_get_additional_image_sizes();
if ( !count($sizes) )
return $fields;
$items = array();
foreach ( array_keys($sizes) as $size ) {
$downsize = image_downsize( $post->ID, $size );
$enabled = $downsize[3];
$css_id = "image-size-{$s}-{$post->ID}";
$label = apply_filters( 'kc_image_size_name', $size );
$html = "<div class='image-size-item'>\n";
$html .= "\t<input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[{$post->ID}][image-size]' id='{$css_id}' value='{$size}' />\n";
$html .= "\t<label for='{$css_id}'>{$label}</label>\n";
if ( $enabled )
$html .= "\t<label for='{$css_id}' class='help'>" . sprintf( "(%d × %d)", $downsize[1], $downsize[2] ). "</label>\n";
$html .= "</div>";
$items[] = $html;
}
$items = join( "\n", $items );
$fields['image-size']['html'] = "{$fields['image-size']['html']}\n{$items}";
return $fields;
}
kc_additional_image_size_input_fields() is the function that will actually insert the custom image sizes list if it the list exists (default), or create it if it doesn’t exist yet (modified by plugins/themes). This function is basically a stripped down version of the built-in image_size_input_fields() function which unfortunately doesn’t provide a filter hook for us to inject our custom image sizes.
The last step is to hook our function into attachment_fields_to_edit so it WordPress will execute it when the media upload lighbox is displayed.
add_filter( 'attachment_fields_to_edit', 'kc_additional_image_size_input_fields', 11, 2 );
As usual, you can add the above code snippets into your themes/plugins, or donwload this plugin, rename the extension to php and upload it to your plugins or mu-plugins folder.
Bugs? Ideas? Feel free to leave your comments below :)


Thanks for this solution. It worked great for me. I will be probably be using this on every WordPress site I create.
I did notice one problem with the code from your plugin download link. I had to add a closing parentheses after “html”.
if ( !isset($fields['image-size']['html'] || substr($post->post_mime_type, 0, 5) != 'image' )I was getting a PHP error without it. Works fine like this:
if ( !isset($fields['image-size']['html']) || substr($post->post_mime_type, 0, 5) != 'image' )Thanks again.
Thanks for the fix Rob. I made some modifications before uploading it and didn’t realize I made a typo. I’ve replaced the file with the fix.
Thanks again and I’m glad you found it useful.
i don’t understand where do you insert the new image sizes? i didn’t see any reference to pixels width or height… how does it do it?
thanks.
Asaf, you will need to call add_image_size() in your theme/plugin before using this plugin.
Awesome plugin. Many thanks for your hard work. Just an idea that would make this plugin even more useful…
It would be great if the custom sizes could be included in the “edit image” functionality in the media library.
I frequently need to custom crop images and it would be fabulous to be able crop my custom image sizes right there within WP.
Thank you for using the plugin Trey.
I have some fixes for this plugin for the next update, and will try to implement your idea too.
+1 to this idea
Hi, I’ve got a problem with this plugin.
I have added a custom size, it shows up when I try to upload something, but I can not click it or see what the size is. Did I specify the size wrong or something?
I’ve now added to functions.php:
add_image_size( “custom”, 630);
which to me would seem like WP takes the image and crops it to a max of 630 px in width.
I am using WP 3.1.4 (because of old PHP version) I don’t know if that’s what is erroring here.
Hi dorxy,
Perhaps you added the size after the image you’re trying to insert have been uploaded?
If so, you need to rebuild the thumbnails for that particular attachment. You can install Regenerate Thumbnails and activate it, go to Media page in Dashboard, click the Regenerate Thumbnails link under the attachment name. After that, the size should show up and you’ll be able to insert it into your post.
Let me know if this works.
PS: You can also rebuild the thumbnails for all image attachments in Tools » Regen. Thumbnails.
Thanks, worked like a charm! :D
I guess other people might run into this problem as well so good thing I posted it ;)
Nice fix, I was looking for this solution for a long time, thx.