I’m building a members website with WordPress Multisite. Is it possible to restrict how many images are being generated depending on the selected template?
I have tried the following lines of code to generate certain images on the gallery template:
// Generate on all uploads
add_theme_support('post-thumbnails');
set_post_thumbnail_size( 1440, 350, true );
add_image_size( 'standard_box', 450, 215, true );
add_image_size( 'default_image', 691, 9999 );
// Generate on gallery template only
if ( is_page_template('page-gallery.php') ) {
add_image_size( 'gallery', 900, 9999 );
add_image_size( 'gallery_thumb', 450, 450, true );
}
This hasn’t worked. I’ve done some research and can’t seem to find anything on the subject. If you could point me in the right direction, I’d really appreciate it.
This has always been a bugbear for me – the lack of on-demand image sizing, and the subsequent number of files you can end up with if you have lots of sizes!
I can see the logic behind your efforts – the trouble is,
add_image_size
only truly comes into play at point-of-upload. As such,is_page_template(..)
will always befalse
.A quick google dug up Aqua Resizer, a script designed to tackle this issue. Rather than use
add_image_size
, you useaq_resize
directly in your theme, and if a size for the image doesn’t exist, it’s created and cached on-the-fly.In fact I’ve used a similar, albeit different, technique on several sites with many image sizes. You still save the overhead of WordPress generating every size for every image uploaded – they’re generated on-the-fly (& cached) as and when they’re requested. Where it differs, is that you can simply use all of WP’s standard image functions and template tags as you would normally!
Also, as @Waqas mentioned, using Aqua Resizer will leave orphaned files when you delete an image from your media library. With my technique, all files will be deleted, since they’re saved to the database & recognised by WordPress.
And in practice:
I’m intending to turn this into a plugin that will automatically convert all
add_image_size
calls into on-demand resizing, so watch this space!Disclaimer:
– This isn’t actually an answer.
– It is intended to help you with your additional research on the topic.
– Furthermore it is reflecting a – at least felt – lately more frequent occurrence of similar questions regarding similar problems.
Additional information regarding this topic on WordPress Development:
Note: The list isn’t ordered and comprehensive by any means.
If you want to create thumbs on the fly you can use Aqua image resizer, But there is a drawback of this mini script. The created thumbs will not delete upon deleting image from library. But its not a big deal. If required you can do so via SHH commands
It is not a direct answer for your problem. But I will help you to make image according to your need.
When you use add_image_size it does not resize the existing images. it only works for the new images that will be uploaded after adding your add_image_size function.
So your code will not generate new images for is_page_template function.
But you can use a simple php class to solve your problem…. it is a famous php class that is being used for a lot of premium theme out there for WordPress. It is called Aqua-Resizer.
You can find more details here https://github.com/syamilmj/Aqua-Resizer
Wiki: https://github.com/syamilmj/Aqua-Resizer/wiki
Problem that may arise:
This function works this way….
So when it can not crop ( for small image that your height or width defined ) it does not show anything. You can overcome this situation by checking whether your are getting a null value after passing the url within this function very easily like mine here….
In this way you can ensure that the specific image will be generated for specific page template and this way your website will be much more cleaner.
P.S: this php class usages WordPress core cropping system so there is not security issues.