本教程是可以清除上传目录中不再存在的图像的所有WP附件元数据。 它检查WordPress postmeta表中的所有行以查找附件元数据中引用的任何图像。 然后,任何引用了上载文件夹中不存在的图像的_wp_attachment_metadata(例如已删除的图像)都将以正确的方式更新,而不是简单地手动更改数据库中的值,而是让WordPress更新序列化的值。

它会检查所有_wp_attachment_metadata值中列出的所有图像,并检查您的“wp-content/uploads”文件夹以查看该图像是否存在。 如果不存在,则将更新WordPress数据库中的元数据,以使用WordPress函数以正确的方式删除该图像数据。

The WordPress Toolbox
Unlimited Downloads: 500,000+ WordPress Themes, Plugins, Templates & Design Assets

删除图像后自动清理wp_attachment_metadata

DOWNLOAD NOW

function isa_cleanup_attachment_metadata(){
 
    if ( get_option( 'my_run_only_once_12' ) != 'completed' ) {
   
        $upload_dir   = wp_upload_dir();
        $upload_basedir = $upload_dir['basedir'];
        $attachment_post_ids = array();
 
        // get all attachment ids for any _wp_attachment_metadata that exists in the wp_postmeta table
         
        // @todo edit database details: 
 
        $mysqli = new mysqli('DB_HOST', 'DB_USER', 'DB_USER_PASSWORD', 'DATABASE_NAME');
 
        if( ! $res = $mysqli->query( "SELECT * FROM wp_postmeta WHERE meta_key = '_wp_attachment_metadata'" ) ) {
            error_log($mysqli->error);
        } else {
            while ($row = $res->fetch_assoc()) {
                $attachment_post_ids[] = $row['post_id'];
            }
            $res->close();
        }
        $mysqli->close();
 
 
        foreach( $attachment_post_ids as $attachment_id ) {
 
            $delete_flag = true;
            $update_flag = false;
 
            $data = wp_get_attachment_metadata( $attachment_id );
 
            $original_file_name = $data['file'] ?? '';
 
            $new_data = $data;
 
            // get all filenames from the data
 
            $all_sizes_filenames = wp_list_pluck( $data['sizes'], 'file' );
 
            foreach( $all_sizes_filenames as $size => $filename ) {
 
                // check that each one doesn't exist, if it does, then don't delete the whole attachment, just update it
 
                if ( file_exists( $upload_basedir . '/' . $filename ) ) {
 
                    $delete_flag = false;
 
                } else {
 
                    unset( $new_data['sizes'][ $size ] );// remove that size from the array
 
                    $update_flag = true;
 
                }
             
            }
 
 
            // if there's no original file, it is not an image, so don't delete the attachment, just update it to remove the sizes
 
 
            if ( $original_file_name && $delete_flag ) {
 
                // none of the sized images exist, now check if original file exists
 
                if ( ! file_exists( $upload_basedir . '/' . $original_file_name ) ) {
             
                    error_log('DELETING attachment id ' . $attachment_id . ' :');
                    error_log(print_r($data, true));
 
                    wp_delete_attachment( $attachment_id );
 
                } 
 
 
            } elseif ($update_flag) {
 
                if ( count( $new_data ) === 1 && empty( $new_data['sizes'] ) ) {
                    $new_data = array();// to make sure empty _wp_attachment_metadata is deleted
                }
 
                error_log('UPDATING attachment metadata for attachement id ' . $attachment_id . ' :');
                error_log(print_r($new_data, true));
 
                wp_update_attachment_metadata( $attachment_id, $new_data );
 
 
            }
 
 
        }// ends foreach attachment
 
 
        update_option( 'my_run_only_once_12', 'completed' );
     
 
    }// end run only once
 
}
 
add_action('admin_init', 'isa_cleanup_attachment_metadata');

 

发表回复