≡ Menu

WordPress Post Thumbnail and Image Functions

Wordpress Post Thumbnail and Image Functions

Working with set_post_thumbnail_size and add_image_size

Today, I would like to post about these two handy functions introduced in WordPress v2.9, these two functions are used to integrate and work with the wordpress post thumbnails features, which was also introduced in wordpress v2.9.

I have been seeing people say that these functions are not working in their theme as they should. But i would like to point out some things that might help you with that and at the same time help you understand what the difference in these two functions are.

set_post_thumbnail_size and add_image_size are not cropping or resizing your images? TOP ↑

The Solution:
First, you need to make sure you have the GD Library installed and enabled in PHP for this to work. I am pretty sure it comes bundled with the default php zip archive that you download from PHP.net, so all you might have to do is enable it in your php.ini configuration file.

When you’re done and absolutely sure that GD is running fine, we move on.

Note:-
Wordpress doesn’t CROP or RESIZE images that have already been uploaded. So if you are trying to crop or resize an image that has been uploaded before you implemented the set_post_thumbnail_size() or add_image_size() functions then you’re not going to get that, instead wordpress will just resize the original and show that to you, it’ll probably be distorted too.

There are 2 things in my mind you can do about that, the first is very obvious you re-upload the image manually, or if there are too many images then it might consume quite some time; so for that we can use the Thumbnail Rebuild Plugin to scan and generate images for the old ones.

The Difference – set_post_thumbnail_size vs add_image_size TOP ↑

Basically, there is not really much of a difference, they’re actually both one in the same, I’ll try to explain more clearly.

add_image_size function description from the wordpress codex.
Registers a new image size. This means that WordPress will create a copy of the post thumbnail with the specified dimensions when you upload a new thumbnail.

The set_post_thumbnail_size() function is the same as add_image_size, but instead of letting you define a name for the thumbnail it sets a default one for you which is named post-thumbnail. That’s the simplest way i can put it for you.

Check out the below code – That is how the set_post_thumbnail_size() function is defined in the wordpress core.

/**
 * Registers an image size for the post thumbnail
 */
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) {
	add_image_size( 'post-thumbnail', $width, $height, $crop );
}
// You can find this @ /wp-includes/media.php at line number 185

As you can see, set_post_thumbnail_size() is using the add_image_size() function. The difference in two is that set_post_thumbnail is setting a default name for you to use.

OK, But How do i implement and use this in my own wordpress theme? TOP ↑

Easy, read my next post How to Use Post Thumbnails Feature in WordPress.

Sorry, i couldn’t add the set_post_thumbnail_size() Function Reference below, as it isn’t available yet. If you think you know something that would be useful for others that could be included in the below list, then do let me know.

Share
{ 2 comments… add one }
  • Dietmar March 15, 2011, 11:18 am

    Thank you so much for the tipp, that Wordpress won’t crop allready existing post thumbnails. That thing gave me headages for days!

  • Zubair March 15, 2011, 5:25 pm

    You’re welcome, I’m glad to hear this helped you out 🙂

Leave a Comment