Skip to content

Instantly share code, notes, and snippets.

@jamigibbs
Created March 23, 2015 22:15
Show Gist options
  • Save jamigibbs/c057fe2d445b7723a665 to your computer and use it in GitHub Desktop.
Save jamigibbs/c057fe2d445b7723a665 to your computer and use it in GitHub Desktop.
Delete retina-ready images
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 );
}
}
}
}
}
@samkent
Copy link

samkent commented Mar 28, 2015

This seems to leave WordPress native image sizes (large, medium, thumbnail) behind when you delete the uploaded image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment