Last active
July 26, 2017 10:54
-
-
Save sabrina-zeidan/bf85aacd7a776867af47a6953247c5cb to your computer and use it in GitHub Desktop.
This function frees disk space on your hosting account. It will scan your uploads directory (recursevely) and check every found file whether it is wp attachment or no. All files that are not attachments (no matter got parents or no) will de deleted. [Wordpress]
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
//Delete orphaned files in wp-content/uploads that are not wordpress attachments | |
function clean_uploads_from_nonattachments(){ | |
$uploads_dir = wp_upload_dir(); | |
$search = $uploads_dir['basedir']; | |
$replace = $uploads_dir['baseurl']; | |
//You may want to take it by bites if your uploads is rather large (over 5 gb for example) | |
//$uploads_dir = ( $uploads_dir['basedir'] . '/2015/' ); | |
$uploads_dir = ( $uploads_dir['basedir']); | |
$root = $uploads_dir; | |
//Going through directiry recursevely | |
$iter = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS), | |
RecursiveIteratorIterator::SELF_FIRST, | |
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" | |
); | |
foreach ($iter as $fileinfo) { | |
//get files only | |
if ($fileinfo->isFile()) { | |
$image = $fileinfo->getPathname(); | |
$image_url = str_replace($search, $replace, $image); | |
//Core WP function to retrieve attachment ID by URL | |
$attachment_id = attachment_url_to_postid($image_url); | |
//Not found - then delete file | |
if (!$attachment_id){ | |
unlink($image); | |
} | |
else { | |
//List of found attachments | |
echo $attachment_id.': '.$image; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment