Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the difference between set_post_thumbnail_size and add_image_size? Is set_post_thumbnail_size for all images, and add_image_size for sizing images individually?

Thanks.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

When an image is uploaded in Wordpress, it is saved in its original size, and also in some resized copies in different sizes, in this way became easy use different size of the same image for different purpose.

By default, WordPress, create 3 copies in 3 different size:

  • 'thumb'
  • 'medium'
  • 'large'

The size in pixel for these 3 sizes can be setted in Wordpress dashboard, under the menu Settings -> Media.

Now, the function add_image_size is used to register a new size, that adds to the 3 defaults.

Every size registered can be retrieved using its name, in some WordPress function, e.g. wp_get_attachment_image_src, wp_get_attachment_image, wp_get_attachment_link.

All these functions accept a parameter $size that should be the name of one of the registered sizes (one of the 3 standards or one of the custom registered with add_image_size).

If 'full' is used as $size argument, is returned the original image, the one not resized.

In addition to the functions mentioned above, there are other 2 functions that make use of image sizes: get_the_post_thumbnail and the_post_thumbnail.

These 2 functions get (the first) and echo (the second) the image that is setted as "Featured Image" for a post.

What is returned (or echoed) by this functions is a full img html tag (something like <img scr="http://www.site.com/wp-content/2013/08/image-200x200.jpg" />) so, which size is used?

If a $size is passed as 2nd argument (1st is $postid) than these functions return the image in that size, otherwise, these functions search for a image size registered with the name: post-thumbnail. This is not one of the 3 default sizes, in fact, it is the 4th standard size (the 5th considering full) and you can register it calling set_post_thumbnail_size.

So, set_post_thumbnail_size($width, $height, $crop) is a sort of shortcut for add_image_size('post-thumbnail', $width, $height, $crop).

If you haven't called set_post_thumbnail_size (which means the size 'post-thumbnail' is not created, WordPress will use the size thumb and if even this size is not available, WordPress will use the original image, the one not resized.

Register custom image sizes, can be very helpful for design a theme, but it's important not abuse: every image is uploaded is copied and resized for all the sizes registered, so, register a lot of sizes will heavily slow down the image uploading process.

A note. Be aware that when WordPress create the scaled copies of an image, never enlarge it, but only make smaller copies: e.g. if the original image is 400x500 px, and the 'medium' image size is 800x600 px, medium size and larger are not created. So register an image size never gives the security that an image file for every size registerd actually exists in wordpress content folder. Even because image size registered easily change: changing setting, switching theme, ecc..

When an image size change, for every reason, the change has effect on the images uploaded after that change, images uploaded before don't change dimensions and are not rescaled and resaved. If one need to rely on specific image sizes, after some images are already uploaded (e.g . after changing theme) then the plugin Regenerate image thumbnail will be life saver.

share|improve this answer
1  
Awesome. I would have clicked 5 times because of your answer but the site does not allow it. Thanls. –  Greeso Aug 1 '13 at 9:18
    
You are welcome, one is ok anymore :) –  G. M. Aug 1 '13 at 9:22
    
Just one more questoin: What if I have not called set_post_thumbnail_size (which means that the size 'post-thumbnail' is not created, then I call the_post_thumbnail. What is going to happen? (because the_post_thumbnail will try to use post-thumbnail as a param). –  Greeso Aug 1 '13 at 10:15
    
@G.M., Thumbs up for a lucid explanation –  Istiaque Ahmed Jan 6 at 8:12

From Codex:

set_post_thumbnail_size

Set the default Featured Image (formerly Post Thumbnail) dimensions. To register additional image sizes for Featured Images use: add_image_size().

add_image_size

Registers a new image size. This means that WordPress will create a copy of the Featured Image (formerly known as post thumbnail) with the specified dimensions when you upload a new image.

share|improve this answer
    
Can I use add_image_size without using set_post_thumbnail_size? –  Greeso Aug 1 '13 at 8:08
    
Yes, of course. –  Krzysiek Dróżdż Aug 1 '13 at 10:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.