Skip to content

Instantly share code, notes, and snippets.

@phpbits
Last active November 17, 2016 04:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phpbits/0d2728d76fb1b822aede to your computer and use it in GitHub Desktop.
Save phpbits/0d2728d76fb1b822aede to your computer and use it in GitHub Desktop.
Automatically replace original uploaded image with large image size
<?php
/*
* Remove Original Uploaded images
* retain large size image
*/
add_filter('wp_generate_attachment_metadata','phpbits_replace_uploaded_image');
function phpbits_replace_uploaded_image($image_data) {
// if there is no large image : return
if (!isset($image_data['sizes']['large'])) return $image_data;
// paths to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
$large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file']; // ** This only works for new image uploads
// delete the uploaded image
unlink($uploaded_image_location);
// rename the large image
rename($large_image_location,$uploaded_image_location);
// update image metadata and return them
$image_data['width'] = $image_data['sizes']['large']['width'];
$image_data['height'] = $image_data['sizes']['large']['height'];
unset($image_data['sizes']['large']);
return $image_data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment