2

I solve one problem, I use in template several different sizes of images (add_image_size), but I use them only for thumbnails, it is completely unnecessary to create these sizes for each image uploaded to WP, I have tens of thousands of images in media. Not sure how to solve this idea? Thanks for any help.

1 Answer 1

1

https://github.com/crstauf/WordPress-FeaturedImage-SpecialImageSize

try this out; wrote it myself a long time ago. I do have an updated version, coming out soon. let me know if you've any questions.

as requested, relevant code for functions.php of your theme:

// `post_type` => array(width,height,crop)
$featimg_sizes = array(
    'post' => array(50,20,false)
);

add_action('wp_ajax_set-post-thumbnail','generate_featimg_size',1);
function generate_featimg_size() {
    global $featimg_sizes;

$thumbnail_id = intval( $_POST['thumbnail_id'] );
$type = get_post_type(intval( $_POST['post_id'] ));

if (!isset($thumbnail_id)) return;
if ('-1' == $thumbnail_id) return;
if (!array_key_exists($type,$featimg_sizes)) return;

$size = $featimg_sizes[$type];
if (!isset($size[0]) || !isset($size[1]) || empty($size[0]) || empty($size[1])) return;
if (!isset($size[2])) $size[2] = false; // set $crop if left blank to false

$meta = wp_get_attachment_metadata($thumbnail_id);
$path = apply_filters('image_make_intermediate_size',get_attached_file($thumbnail_id));

if ($newsize = image_make_intermediate_size($path,$size[0],$size[1],$size[2])) {
    $backupsizes = get_post_meta($thumbnail_id,'_wp_attachment_backup_sizes',true);
    $meta['sizes']['ptfi_'.$type] = $backupsizes['ptfi_'.$type] = $newsize; // ptfi = post type featured image

    update_post_meta($thumbnail_id,'_wp_attachment_metadata',$meta);
    update_post_meta($thumbnail_id,'_wp_attachment_backup_sizes',$backupsizes);
}
}
`
6
  • 1
    Please edit your answer to include the most relevant code. External link can not be trusted not to disappear at some point. Sep 14, 2014 at 3:41
  • posting code can not be trusted to remain relevant to the current WordPress version. what should be done?
    – Caleb
    Sep 14, 2014 at 21:14
  • Awesome Caleb, this is great solution and works well. Thank you. Sep 15, 2014 at 9:04
  • @MichalPelech, do you mind voting up my answer? trying to gain some rep.
    – Caleb
    Sep 15, 2014 at 14:27
  • @Caleb I wanted to, but I do not have the required amount of reputation. Sep 16, 2014 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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