I’m worried about too many files created when adding a new image size.
All uploaded files will generate the new size, I need a specific image size only for the file set as Fetured image, is there some way to do this?.
The thumbnail, medium, etc are ok, but there is no need to create the new size for every file.
I think this should work right after setting the Featured Image.
Research
The question Filter For Featured Image lead to this answer: How to hook update_post_meta and delete_post_meta?.
Coupled with the a fine global variable (
$_wp_additional_image_sizes
) revealed here: How to get a list of all the possible thumbnail sizes set within a theme, bring us to the code that catches the “Use as featured image” action click.Code
This code is fired at each “Use as featured image” click. It then goes through all Additional Image Sizes and deletes them, keeping the default WordPress ones (thumbnail, medium, large).
Test this code throughly before going LIVE.
GPL Code. No warranty granted. Check the comments.
Result
Folder contents after uploading images, no Feature Image set yet
Folder contents after setting
fondo-restauraciones
as the Featured ImageOther Notes
For dealing with ALL extra image sizes (WordPress defaults and custom defined) use:
You can get the featured image
src
/source by using the following arguments on the core function (where$post
, should be called withglobal $post
in front):How-to use it
I wrote a free plugin available on GitHub, called “Dynamic Image Resize”.
You can download & use it for free.
Short Code
Place
[dynamic_image]
in your content. The shortcode has four arguments:src
Full path to the image in your upload directory or the IDwidth
Integer valueheight
Integer valueclasses
Css classes â separated by a space… but there’s also a:
Template Tag
Just dump it in your template where you need it and finito.
Note: It’s based on an idea/proposal by Konstantin Kovshenin.
Sidenote:
If you want to skip/disable default image sizes by default, just add
0
aswidth
andheight
in the admin settings.There is now also a plugin solution available that can elegantly handle this issue. It’s called Fly Dynamic Image Resizer, and dynamically creates the image when it is called for the first time, rather than when it is uploaded.
From the plugin’s description:
The way it works is like this:
The plugin looks well supported at the time of writing.