Skip to content

Instantly share code, notes, and snippets.

@mcgregormedia
Created March 25, 2015 12:38
Show Gist options
  • Save mcgregormedia/56eecea0c980ae48cc81 to your computer and use it in GitHub Desktop.
Save mcgregormedia/56eecea0c980ae48cc81 to your computer and use it in GitHub Desktop.
add_filter( 'delete_attachment', 'delete_retina_support_images' );
function delete_retina_support_images( $attachment_id ) {
$meta = wp_get_attachment_metadata( $attachment_id );
$upload_dir = wp_upload_dir();
$path = pathinfo( $meta['file'] );
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