Created
March 23, 2015 22:15
-
-
Save jamigibbs/c057fe2d445b7723a665 to your computer and use it in GitHub Desktop.
Delete retina-ready images
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
add_filter( 'delete_attachment', 'delete_retina_support_images' ); | |
/** | |
* Delete retina-ready images | |
* | |
* This function is attached to the 'delete_attachment' filter hook. | |
* Reference: http://code.tutsplus.com/tutorials/ensuring-your-theme-has-retina-support--wp-33430 | |
*/ | |
function delete_retina_support_images( $attachment_id ) { | |
$meta = wp_get_attachment_metadata( $attachment_id ); | |
$upload_dir = wp_upload_dir(); | |
if( isset( $meta['path'] ) ) { | |
$path = pathinfo( $meta['path'] ); | |
foreach ( $meta as $key => $value ) { | |
if ( 'sizes' === $key ) { | |
foreach ( $value as $sizes => $size ) { | |
$original_filename = $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size['file']; | |
$retina_filename = substr_replace( $original_filename, '@2x.', strrpos( $original_filename, '.' ), strlen( '.' ) ); | |
if ( file_exists( $retina_filename ) ) | |
unlink( $retina_filename ); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seems to leave WordPress native image sizes (large, medium, thumbnail) behind when you delete the uploaded image.