Add custom image sizes, the right way

Since version 2.9, WordPress supports custom image sizes that will be used to create thumbnails of image attachments. Adding new image sizes is pretty easy; in your theme’s functions.php file, add this block of code:

function mytheme_add_image_sizes() {
    add_image_size( 'small_thumb', 50, 50, true );
}
add_action( 'after_setup_theme', 'mytheme_add_image_sizes' );

… and you’re done. Now, everytime a new image is uploaded, a 50×50 thumbnail with be created for you.

However, this new image size won’t show up when you want to insert an image into a post:

Image size dropdown on Insert Media frame
Image size dropdown on Insert Media frame

The solution is pretty simple too; add this bit of code into your theme’s functions.php file:

function mytheme_image_size_names( $sizes ) {
    $sizes['small_thumb'] = __( 'Small Thumb', 'mytheme' );

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

Now you can select your custom image size from the dropdown:

Image size dropdown on Insert Media frame, with custom image sizes.
Image size dropdown on Insert Media frame, with custom image sizes.

Notes:

Add custom image sizes, the right way

2 thoughts on “Add custom image sizes, the right way

  1. Diana says:

    I can’t seem to get this additional code into my theme so that I can get to the small thumbnail. The plugin works great but I can’t get the icon image small enough. I am a novice at this and may be putting the code in the wrong place. Any assistance would be appreciated.

    1. Dzikri Aziz says:

      To use a different size, you can change `50` on the code above to whatever number you need. Don’t forget to rebuild the thumbnails for existing images using the mentioned plugin above.

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.