Skip to content

Instantly share code, notes, and snippets.

@victormattosvm
Created March 30, 2021 07:49
Show Gist options
  • Save victormattosvm/a82738594f758f373414c43a80058320 to your computer and use it in GitHub Desktop.
Save victormattosvm/a82738594f758f373414c43a80058320 to your computer and use it in GitHub Desktop.
<?php
/*
* CAUTION!
* This code will delete all generated thumbnails. Useful if you want to regenerate.
* Don't forget to regenerate thumbnails with wp-cli or any plugin.
*/
add_action( 'init', 'remove_all_thumbnails' );
function remove_all_thumbnails(){
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
) );
foreach ($attachments as $attach){
$filePath = get_attached_file($attach->ID);
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
$dirname = pathinfo($filePath, PATHINFO_DIRNAME);
$filename = basename($filePath,".".$ext);
foreach (glob("{$dirname}/{$filename}-*") as $delete_file) {
unlink($delete_file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment