set_post_thumbnail_size( 80, 80, true ); // true for all post types
Suppose I have two post types in play on my blog: Post, Video and Product. I may want a 80×80 image size for my posts, but 100×100 for my videos and a 50×100 for my Products. To my knowledge, I am forced to create thumbnails for my Post images in the dimensions of 100×100 and also 50×100. And for my products, I’m forced to create thumbnails that are 80×80 and 100×100, even though I won’t use those ever.
Is there a way to create image sizes that are based on post types so you don’t end up making a bunch of images you don’t plan on using, and wasting resources that could be used elsewhere?
There is no way to base the image sizes on the post type, as images are not yet attached to a post when you upload them and the intermediate image sizes are created (when you just add them via “New Media” they are not even attached to any post at all).
However, I recently created two plugins that together will do what you want. Virtual intermediate images will intercept the creation of the intermediate image sizes: they will not be created on disk, but still exist in the metadata array. This makes WordPress think they exist so you can use them like normal images. The companion plugin, On-Demand Resizer will create missing image sizes when they are requested. The first time the image is requested it is created, all subsequent requests will just be served the newly created image.
These plugins need a rewrite before I put them in the WordPress plugin directory, but they should be safe. Or at least have
TODO
comments where they are unsafe.I posted some code on this other thread which sets a transient with the post type when uploading an image, and then checks for the value of that transient and adjusts the post-thumbnail size accordingly when the upload is finished. Its not perfect or bulletproof, but it should demonstrate the concept at least.
I came up with following solution. It does create images only for custom post type without creating any additional ones. The only problem is that if you use plugins like advanced custom fields, when trying to render the gallery field it only shows the default wordpress images sizes: ‘thumbnail’, ‘medium’, ‘large’ and you cannot access custom post image sizes created.
}