Created
March 11, 2023 21:27
-
-
Save abracadabra80/25f2e627b4f6239cddfb4ebb33876b20 to your computer and use it in GitHub Desktop.
Remove old images
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 | |
| /* | |
| Plugin Name: Clean Database | |
| Plugin URI: http://example.com/clean-database | |
| Description: A plugin to clean database by removing post attachments and their generated images older than 180 days | |
| Version: 1.0 | |
| Author: chatgpt | |
| Author URI: http://example.com | |
| License: GPL2 | |
| */ | |
| // Add the menu item to Tools menu | |
| function clean_database_add_menu_item() { | |
| add_submenu_page( | |
| 'tools.php', | |
| 'Clean Database', | |
| 'Clean Database', | |
| 'manage_options', | |
| 'clean_database', | |
| 'clean_database_page' | |
| ); | |
| } | |
| add_action('admin_menu', 'clean_database_add_menu_item'); | |
| // The function that is called when the "Clean Now" button is clicked | |
| function clean_database() { | |
| global $wpdb; | |
| // Delete old attachments | |
| $wpdb->query( | |
| $wpdb->prepare( | |
| "DELETE p, pm FROM $wpdb->posts p | |
| LEFT JOIN $wpdb->postmeta pm ON pm.post_id = p.ID | |
| WHERE p.post_type = 'attachment' | |
| AND DATEDIFF(NOW(), p.post_date) > 180" | |
| ) | |
| ); | |
| // Remove the actual image and its generated sizes from the uploads folder | |
| $upload_dir = wp_upload_dir(); | |
| $attachments = $wpdb->get_results( | |
| "SELECT * FROM $wpdb->posts WHERE post_type = 'attachment'" | |
| ); | |
| foreach ($attachments as $attachment) { | |
| $file = get_attached_file($attachment->ID); | |
| if (file_exists($file)) { | |
| unlink($file); | |
| $metadata = wp_get_attachment_metadata($attachment->ID); | |
| if (!empty($metadata)) { | |
| foreach ($metadata['sizes'] as $size => $data) { | |
| $image_file = path_join($upload_dir['basedir'], $data['file']); | |
| if (file_exists($image_file)) { | |
| unlink($image_file); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Display success message | |
| echo '<div class="updated"><p>Database cleaned successfully.</p></div>'; | |
| } | |
| // The main function that displays the "Clean Database" page | |
| function clean_database_page() { | |
| ?> | |
| <div class="wrap"> | |
| <h1>Clean Database</h1> | |
| <p>This tool scans the database tables wp_posts and wp_postmeta for attachments to posts which are older than 6 months and removes all trace.</p> | |
| <p>It also scans and removes the actual image with any generated image size from the uploads folder.</p> | |
| <form method="post"> | |
| <?php wp_nonce_field('clean_database_nonce', 'clean_database_nonce'); ?> | |
| <input type="submit" name="clean_database_button" class="button button-primary" value="Clean Now"> | |
| </form> | |
| <?php | |
| if (isset($_POST['clean_database_button'])) { | |
| // Verify the nonce | |
| if (!wp_verify_nonce($_POST['clean_database_nonce'], 'clean_database_nonce')) { | |
| die('Security check failed.'); | |
| } | |
| clean_database(); | |
| } | |
| ?> | |
| </div> | |
| <?php | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment