Last active
September 6, 2020 11:39
-
-
Save bryanwillis/2c4a95e6ae0b41080b23 to your computer and use it in GitHub Desktop.
delete image references to the wordpress database when they have been deleted from from the uploads folder and not from the admin interface
This file contains hidden or 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 | |
// Only run the code if we are in the admin | |
if ( is_admin() ) : | |
class WordPressdeleteMissingImages { | |
// Action/Filter Hooks | |
function __construct() { | |
add_action( 'admin_menu', array( &$this, 'add_page' ) ); | |
add_action( 'admin_init', array( &$this, 'admin_init' ) ); | |
} | |
function admin_init() { | |
if ( array_key_exists( 'delete-missing-images', $_GET ) ) { | |
$imgs = get_posts("post_type=attachment&numberposts=-1"); | |
foreach($imgs as $img){ | |
$file = get_attached_file($img->ID); | |
if(!file_exists($file)){ | |
wp_delete_post( $img->ID, false ); | |
} | |
} | |
wp_redirect( admin_url() . '?missing-images-deleted' ); | |
exit(); | |
} | |
else if ( array_key_exists( 'missing-images-deleted', $_GET ) ) | |
add_action( 'admin_notices', array( &$this, 'ok_notice' ) ); | |
} | |
// admin_notices action hook operations | |
// Inform the user that the dirty work is done | |
function ok_notice() { | |
echo '<div id="message" class="updated fade"><p><strong>Missing Images deleted.</strong></p></div>'; | |
} | |
// admin_menu action hook operations | |
// Add the delete menu item | |
function add_page() { | |
global $submenu; | |
if ( current_user_can( 'level_10' ) && function_exists( 'add_management_page' ) ) | |
$submenu['upload.php'][667] = array( 'delete Missing Images', 'manage_options' , admin_url() . '?delete-missing-images' ); | |
} | |
} | |
$WordPressdeleteMissingImages = new WordPressdeleteMissingImages(); | |
endif; |
This file contains hidden or 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 | |
if ( basename( $_SERVER['SCRIPT_FILENAME'] ) == basename( __FILE__ ) ) | |
die( "Access denied." ); | |
/** | |
* Overwrites uploaded files that already exist, instead of storing multiple copies. | |
*/ | |
class OverwriteUploads { | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
add_filter( 'wp_handle_upload_prefilter', array( $this, 'remove_existing_attachment' ) ); // Not really the appropriate hook, but there isn't an action that fits | |
} | |
/** | |
* Remove a existing attachment when uploading a new one with the same name in the same folder | |
* | |
* @param array $file | |
* @return array The unmodified file | |
*/ | |
public function remove_existing_attachment( $file ) { | |
$uploads_dir = wp_upload_dir(); | |
if ( file_exists( $uploads_dir['path'] . DIRECTORY_SEPARATOR . $file['name'] ) ) { | |
$params = array( | |
'numberposts' => 1, | |
'post_type' => 'attachment', | |
'meta_query' => array( | |
array( | |
'key' => '_wp_attached_file', | |
'value' => trim( $uploads_dir['subdir'] . DIRECTORY_SEPARATOR . $file['name'], DIRECTORY_SEPARATOR ) | |
) | |
) | |
); | |
$existing_file = get_posts( $params ); | |
if ( isset( $existing_file[0]->ID ) ) { | |
wp_delete_attachment( $existing_file[0]->ID, true ); | |
} | |
} | |
return $file; | |
} | |
} // end OverwriteUploads | |
This file contains hidden or 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 | |
$imgs = get_posts("post_type=attachment&numberposts=-1"); | |
foreach($imgs as $img){ | |
$file = get_attached_file($img->ID); | |
if(!file_exists($file)){ | |
wp_delete_post( $img->ID, false ); | |
} | |
} | |
This file contains hidden or 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 | |
add_filter( 'posts_where', 'devplus_wpquery_where' ); | |
function devplus_wpquery_where( $where ){ | |
global $current_user; | |
if( is_user_logged_in() && is_admin() && !current_user_can('update_plugins') ) { | |
if( isset( $_POST['action'] ) && ( $_POST['action'] == 'query-attachments' ) ){ | |
$where .= ' AND post_author='.$current_user->data->ID; | |
} | |
} | |
return $where; | |
} | |
add_action('pre_get_posts','users_own_attachmentsd'); | |
function users_own_attachmentsd( $wp_query_obj ) { | |
global $current_user, $pagenow; | |
if( !is_a( $current_user, 'WP_User') ) | |
return; | |
if( 'upload.php' != $pagenow ) | |
return; | |
if( !current_user_can('update_plugins') ) | |
$wp_query_obj->set('author', $current_user->id ); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this code still work? How do I use it? It is exactly what I am looking for.