Skip to content

Instantly share code, notes, and snippets.

@Arty2
Created March 6, 2014 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arty2/9390440 to your computer and use it in GitHub Desktop.
Save Arty2/9390440 to your computer and use it in GitHub Desktop.
Raw utility to remove all thumbnails files from WordPress's upload directory. Edit as necessary.
<?php
/**
* raw utility to delete deprecated thumbnail files
* to move into a plugin at some point
*/
// comment following line and move to WordPress root
if ( !defined('ABSPATH') ) die;
function files($base) {
$root = scandir($base);
foreach($root as $dir) {
if ( $dir === '.' || $dir === '..') {
continue;
}
if ( is_file("$base/$dir") ) {
$result[] = "$base/$dir";
continue;
}
foreach( files("$base/$dir") as $dir) {
$result[]=$dir;
}
}
return $result;
}
$uploads = '/media'; // by default: /wp-content/uploads
$files = files( dirname(__FILE__) . $uploads );
$thumbnails = array();
foreach ($files as $key => $file ) {
if ( 1 == preg_match('#-\d+x\d+\.#', $file) ) {
$thumbnails[] = $file;
// uncomment following line to delete files
// unlink($file);
}
}
echo '<pre>';
print_r($thumbnails);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment