How to delete unused images?
-
Hi,
Does anyone have a good method to delete unused images or which have been replaced through the Magic Fields image field?
It’s really bothersome to have to check all the images one by one and sometimes when I upload different versions of the same image with only slight changes it’s quite complicated to track which was the one finally used.
Does anyone know of some other plug-in or of some function that can be used to automatically scan the uploaded image directory, compare it with the site’s database and track images that aren’t being used anymore?
Thanks!
-
I am searching too.
Maybe this can do the job
https://www.nicearma.com/delete-not-used-image-wordpress-dnui/Have not tested yet
try this SQL command:
select meta_value from wp_postmeta
where meta_key in (select name from wp_mf_custom_fields
where type = ‘image’)
and post_id not in (select ID from wp_posts
where post_status = ‘publish’ or post_status = ‘draft’
or post_status = ‘auto-draft’);——————————————————————–
select name from wp_mf_custom_fields where type = ‘image’
gives field names of fields of type image
——————————————————————–
select ID from wp_posts where post_status = ‘publish’
or post_status = ‘draft’ or post_status = ‘auto-draft’
gives IDs of posts in publish or draft status
——————————————————————–
So we are selecting the value of fields of type image which do not
have a post that is currently published or in draft form.
These values are the file names of files in the wp_content/files_mf
folder. Alternatively you can try using post_status = ‘trash’Although I haven’t tried either Nicearma or Upload Janitor I strongly suspect that they will not work because Magic Fields use its own proprietary way of storing images for fields of type ‘image’. (Images of type ‘image_media’ are stored in the standard WordPress way.) In particular, Magic Field images are stored in the Magic Field’s specific folder ‘wp_content/files_mf’. Without code specific to Magic Fields a generic utility would not know about this Magic Field specific folder. It would be surprising if these generic utilities have Magic Field specific logic coded in it.
My previous SQL command will not work as wp_content/files_mf
folder contains file with no corresponding entry in the SQL database.However,
select meta_value from wp_postmeta
where meta_key in (select name from wp_mf_custom_fields
where type = ‘image’)
and post_id in (select ID from wp_posts
where post_status = ‘publish’ or post_status = ‘draft’
or post_status = ‘auto-draft’);should give you a list of currently in use images so the image files in wp_content/files_mf NOT in this list are the ones you want to delete.
/* * Finds files in folder "wp-content/files_mf" that are not referenced by * published or draft posts. The file names are displayed in a panel on the * page shown by the "Settings/Magic Fields" menu item of the dashboard. * To install paste this into your functions.php file. * * Note that the unlink is intentionally DISABLED. You need to edit the * appropiate lines to enable it after verifing the correctness of this. */ function get_unreferenced_image_files() { global $wpdb; echo( '<h2>Unreferenced Files in Folder "wp-content/files_mf/"</h2>' ); if ( $handle = opendir( MF_FILES_DIR ) ) { $entries = []; while ( false !== ($entry = readdir( $handle ) ) ) { if ( is_dir( MF_FILES_DIR . $entry ) ) { continue; } $entries[] = $entry; } closedir($handle); echo( '<a href="#" onclick="document.getElementById(\'dir-files-mc\')' . '.style.display=\'block\';return false;"><h3>Files of Folder ' . '"wp-content/files_mf/"' . '</h3></a><div id="dir-files-mc" style="border:2px solid black;' . 'margin:10px 20px;padding:5px;display:none;"><button style' . '="float:right;" onclick="this.parentNode.style.display=\'none\';' . 'return false;">X</button><ol>' ); foreach ( $entries as $entry ) { echo( "<li>\"$entry\"</li>" ); } echo( '</ol></div>' ); $sql = 'SELECT post_id, meta_key, meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key IN (SELECT name FROM ' . MF_TABLE_CUSTOM_FIELDS . ' WHERE type = "image" OR type = "audio" OR type = "file" )' . ' AND post_id IN (SELECT ID FROM '. $wpdb->posts . ' WHERE post_status = "publish" OR post_status = "draft"' . ' OR post_status = "auto-draft")'; $results = $wpdb->get_results( $sql, ARRAY_A ); echo( '<a href="#" onclick="document.getElementById(\'dir-referenced-mc\')' . '.style.display=\'block\';return false;"><h3>Referenced by Published ' . 'or Draft Posts' . '</h3></a><div id="dir-referenced-mc" style="border:2px solid black;' . 'margin:10px 20px;padding:5px;display:none;"><button style' . '="float:right;" onclick="this.parentNode.style.display=\'none\';' . 'return false;">X</button><ol>' ); $referenced = []; foreach ($results as $result) { if ( !$result['meta_value'] ) { continue; } $referenced[] = $result['meta_value']; echo( '<li><a href="' . MF_FILES_URL . $result['meta_value'] . '" target=' . '"_blank"><span style="font-weight:bold;">"' . $result['meta_value'] . '"</span></a> referenced by <a href="' . get_permalink( $result['post_id'] ) . '" target="_blank">' . '<span style="font-weight:bold;">"' . get_the_title( $result['post_id'] ) . '"</span></a> ' . ' via field <span style="font-weight:bold;">' . $result['meta_key'] . '</span></li>' ); } echo( '</ol></div>' ); $unreferenced = array_merge( array_diff( $entries, $referenced ) ); echo( '<a href="#" onclick="document.getElementById(' . '\'dir-unreferenced-mc\').style.display=\'block\';return false;"><h3>' . 'Unreferenced by Published or Draft Posts' . '</h3></a><div id="dir-unreferenced-mc" style="border:2px solid black;' . 'margin:10px 20px;padding:5px;display:block;"><button style' . '="float:right;" onclick="this.parentNode.style.display=\'none\';' . 'return false;">X</button><ol>' ); echo( '<form method="post" action="' . get_option('siteurl') . '/wp-admin/options-general.php?page=get_unreferenced_files_mc&' . 'noheader=true"><ol>' ); foreach ( $unreferenced as $i => $unreference ) { echo( '<li><input type="checkbox" name="to-be-deleted-' . $i . '" value="' . $unreference . '"> "' . $unreference . '"</li>' ); } echo( '</ol><input type="submit" value="Delete Checked"></form></div>' ); } } if ( is_admin() ) { if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin/options-general.php?page=mf_settings' ) !== FALSE ) { function do_delete_notices_mc() { if ( $deleted = get_transient( 'deleted_files_mc17' ) ) { echo( "<div style=\"padding:0px 20px;border:1px solid red;margin:20px;\">$deleted</div>" ); delete_transient( 'deleted_files_mc17' ); } } add_action( 'admin_notices', 'do_delete_notices_mc' ); function do_notices_mc(){ echo( "<div style=\"clear:both;padding:20px;border:2px solid black;margin:20px;\">" ); get_unreferenced_image_files(); echo( '</div>' ); } add_action( 'settings_page_mf_settings', 'do_notices_mc', 11 ); } if (strpos( $_SERVER['REQUEST_URI'], 'wp-admin/options-general.php?page=get_unreferenced_files_mc' ) !== FALSE ) { function auxiliary_menu_mc() { global $_registered_pages; function delete_unreferenced_files_mc() { $deleted='<h3>Status of File Delete Requests</h3><ul>'; $unlinked = 0; $not_unlinked = 0; foreach ( $_REQUEST as $key => $request ) { if ( strpos( $key, 'to-be-deleted-' ) !== 0 ) { continue; } ############################################################################################ ############################################################################################ # unlink is disabled! Uncomment the next line and delete the line after it to enable unlink. # if ( unlink( MF_FILES_DIR . $request ) ) { if ( FALSE ) { $status = '<span style="color:green;">deleted</span>'; ++$unlinked; } else { $status = '<span style="color:red;">failed</span>'; ++$not_unlinked; } $deleted .= "<li>$status - \"" . MF_FILES_DIR . "$request\"</li>"; } $deleted .= "</ul><h3><span style=\"color:green;\">deleted $unlinked files</span>, " . " <span style=\"color:red;\">failed $not_unlinked files</span>.</h3>"; set_transient( 'deleted_files_mc17', $deleted, 10 ); wp_redirect( 'options-general.php?page=mf_settings' ); exit(); } $hookname = get_plugin_page_hookname( 'get_unreferenced_files_mc', 'options-general.php' ); add_action( $hookname, 'delete_unreferenced_files_mc' ); $_registered_pages[$hookname] = true; } add_action( 'admin_menu', 'auxiliary_menu_mc' ); } }
I have put the above code into a plugin to make it easier to use.
- The topic ‘How to delete unused images?’ is closed to new replies.