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.

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.

Insert Image with Custom Size into Post

18 thoughts on “Insert Image with Custom Size into Post

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

    1. kucrut says:

      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.

  2. 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?

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

    1. kucrut says:

      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.

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

    1. kucrut says:

      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.

  5. 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}”;

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

    1. Dzikri Aziz says:

      Hi Doug,

      The code example is outdated. Please download the updated plugin from WordPress’ extend. Also, with the update, image files that don’t have certain sizes won’t show up anymore, instead of having their radio boxes disabled.

      1. Ah, ok, got it, thanks. So, 2 ?s. First your plugin inserts a test img size into wp. Is there a reason it has to remain enabled? What is the reason for it in the first place? Second, In my insertion dialog and also in ‘AJAX Thumbnail Rebuild plugin’ (i find more reliable than ‘Regenerate Thumbnails’) I see a 0x0 size. Is this normal?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.