Last active
October 7, 2015 20:03
-
-
Save davidvandenbor/c81db821d2ab05aa966e to your computer and use it in GitHub Desktop.
WordPress: Auto-replace uploaded image with large image size thumbnail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author @david | |
* replace the WordPress "large" image with the uploaded image | |
* put this in your Wordpress functions.php file: | |
*/ | |
function 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 - fixed for older images below. | |
$large_image_location = $upload_dir['basedir'] . '/'.$image_data['sizes']['large']['path']; | |
// 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; | |
} | |
add_filter('wp_generate_attachment_metadata','replace_uploaded_image'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment