New WordPress Plugin: Custom Image Sizes

I make a lot of WordPress themes, and frequently clients want to associate a particular size of image with a post. You can do this easily with WordPress by using add_image_size() to define an image and then by calling wp_get_attachment_image() later to print the markup for that image.

So for example, if I have an attachment image of ID number 123, I might do something like the following:

add_image_size( 'my-custom-size', 220, 180, true );
echo wp_get_attachment_image( 123, 'my-custom-size' );

Here, add_image_size() defines the custom thumbnail (in this example the arguments tell add_image_size() to make it 220 pixels wide, 180 high, and cropped), and

echo wp_get_attachment_image()

prints the markup of the image itself, <img> element and everything.

The Problem

This works great; WordPress even creates thumbnails in this size from now on. The problem is that it doesn’t apply to pre-existing thumbnails. And if people change their minds about what sizes they want their thumbnails to be (clients sometimes change their minds) you’re stuck with existing thumbnails of the wrong size.

In addition, if you call wp_get_attachment_image( 123, 'my-custom-size' ); and that size doesn’t exist, WordPress just scales down the larger-sized original image, which might cause some performance issues.

Solution: Custom Image Sizes

My solution is the Custom Image Sizes plugin. You activate it, and if you call wp_get_attachment_image() and related functions for an attachment that doesn’t have that size, WordPress will create it on demand.

As a bonus, if you pass a width and height of the desired image to wp_get_attachment_image() (and related functions), you can create any size image. So for example I could create a thumbnail 50 pixels wide by 40 high of attachment 123 with the following code, where '50x40' is '[width]x[height]':

echo wp_get_attachment_image( 123, '50x40' );


You can download the Custom Image Sizes plugin here.


  1. Hi. Great idea for a plugin! Had a quick feature request. Enabling the function to be able to create a cropped image would be ideal. I believe that extending:

    wp_get_attachment_image( 123, '50x40' );


    wp_get_attachment_image( 123, '50', '40', true );

    Would accomplish this rather well.

  2. That’s not a bad idea, Michael, but the problem is that wp_get_attachment_image() is a WordPress function; my plugin just filters some of the data it retrieves. So I can’t change the meaning or order of the function’s arguments.

    By default it crops the image, so maybe I could change it to accept just one of the dimensions to scale it, sorta like ImageMagick syntax:

    wp_get_attachment_image( 123, '50x' );

    I’ll have to think about that.

  3. Can you say more about configuring files to use your plugin?

    Say you have a magazine theme that uses 3 sizes as posts progress through 3 positions (based on age): 1)300×200 for position 1; 2) 260×80 for position 2; 3) 100×100 for position 3.

    So now — how you would configure your theme to crop correctly, and display in the page?

    Thank you

  4. great work! i’m calling attachment ID with this function:
    function attachment_ID(){
    // via mediathek
    $attachments = get_children( array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => 1, // show all -1
    'post_status' => 'inherit',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order ASC'
    ) );
    foreach ( $attachments as $attachment_id => $attachment ) {
    return $attachment_id;

  5. This is very handy, thx for sharing this, much appriciated!

  6. Hi Austin, I’m getting :

    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /wp-content/plugins/custom-image-sizes.php on line 13

    Any ideas what might be causing that?

  7. kwhitedesign,

    It sounds like you’re running PHP 4. My plugin requires that you be using at least PHP 5. (PHP 4 was deprecated in 2008 and no longer receives security patches. You should upgrade as soon as possible.)

    By the way, did you rename the file?

Post a Comment

Your email is never shared. Required fields are marked *


10 Trackbacks