Created
January 15, 2016 03:28
-
-
Save cimmwolf/5ea6698bedaae5aab648 to your computer and use it in GitHub Desktop.
Deletes Wordpress's resized images copies. Make site backup, put file in web root catalog and execute. Require PHP >= 5.4.
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
<?php | |
function formatBytes($bytes) | |
{ | |
if ($bytes >= 1073741824) | |
$bytes = number_format($bytes / 1073741824, 2) . ' GB'; | |
elseif ($bytes >= 1048576) | |
$bytes = number_format($bytes / 1048576, 2) . ' MB'; | |
elseif ($bytes >= 1024) | |
$bytes = number_format($bytes / 1024, 2) . ' KB'; | |
elseif ($bytes > 1) | |
$bytes = $bytes . ' bytes'; | |
elseif ($bytes == 1) | |
$bytes = $bytes . ' byte'; | |
else | |
$bytes = '0 bytes'; | |
return $bytes; | |
} | |
require_once __DIR__ . '/wp-blog-header.php'; | |
global $wpdb; | |
$uploadDir = wp_upload_dir(); | |
$table = $wpdb->postmeta; | |
$rows = $wpdb->get_results("SELECT * FROM $table WHERE meta_key = '_wp_attachment_metadata'"); | |
$freedSpace = 0; | |
foreach ($rows as $row) { | |
$temp = unserialize($row->meta_value); | |
$fileDir = $uploadDir['basedir'] . '/' . dirname($temp['file']); | |
if (isset($temp['sizes'])) { | |
foreach ($temp['sizes'] as $size => $info) { | |
$filePath = $fileDir . '/' . $info['file']; | |
$freedSpace += filesize($filePath); | |
echo 'deleted: ' . $filePath . PHP_EOL; | |
unlink($filePath); | |
} | |
unset($temp['sizes']); | |
$data = ['meta_value' => serialize($temp)]; | |
$wpdb->update($table, $data, ['meta_id' => $row->meta_id]); | |
} | |
} | |
echo 'Total freed space: ' . formatBytes($freedSpace) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment