Insert Image with Custom Size into Post

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.

function my_insert_custom_image_sizes( $sizes ) {
  global $_wp_additional_image_sizes;
  if ( empty($_wp_additional_image_sizes) )
    return $sizes;

  foreach ( $_wp_additional_image_sizes as $id => $data ) {
    if ( !isset($sizes[$id]) )
      $sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) );
  }

  return $sizes;
}
add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );

The result

Note: The new code is adapted from here.

Standard

Comments

  1. Howdy, why are you doing this
    $downsize = image_downsize( $post->ID, $size );
    $enabled = $downsize[3];
    It seems like you just want to know if it is an intermediate size, and havent you already found that our with your first func? And then, what would be the situation for having a disabled radio button anyway?
    Thanks man.

  2. thank you sharing… BTW you have a typo error on this
    $css_id = “image-size-{$s}-{$post->ID}”; instead of $css_id = “image-size-{$size}-{$post->ID}”;

  3. 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.

  4. Trey says:

    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.

  5. asaf chertkoff says:

    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.

  6. 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.