Created
April 28, 2024 02:26
-
-
Save homu9/d90cda84ad545bf678abbd9ab825fa33 to your computer and use it in GitHub Desktop.
Bulk delete WordPress thumbnails
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 | |
require_once('./wp-load.php'); | |
$per_page = 5000; | |
$page = isset($_GET['page']) ? intval($_GET['page']) : 1; | |
$args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'inherit', | |
'posts_per_page' => $per_page, | |
'offset' => ($page - 1) * $per_page, | |
); | |
// Filter by attachment title | |
if (isset($_GET['attachment_title'])) { | |
$attachment_title = sanitize_text_field($_GET['attachment_title']); | |
if (!empty($attachment_title)) { | |
$args['s'] = $attachment_title; | |
} | |
} | |
$attachments_query = new WP_Query($args); | |
$attachments = $attachments_query->posts; | |
$total_attachments = $attachments_query->found_posts; | |
echo $total_attachments; | |
$total_thumbnails = 0; | |
// Delete selected thumbnails | |
if (isset($_POST['delete_thumbnails'])) { | |
if (isset($_POST['attachment']) && is_array($_POST['attachment'])) { | |
$attachments_to_delete = $_POST['attachment']; | |
foreach ($attachments_to_delete as $attachment_id) { | |
$thumbnail_count = 0; | |
$attachment_name = basename( get_attached_file( $attachment_id ) ); | |
$attachment_name = pathinfo($attachment_name, PATHINFO_FILENAME); | |
$attachment_folder = dirname( get_attached_file( $attachment_id ) ); | |
$thumbnail_ids = array(); | |
// Search for thumbnails with the same name as the attachment | |
$thumbnail_files = glob( $attachment_folder . '/' . $attachment_name . '-*' ); | |
foreach ( $thumbnail_files as $thumbnail_file ) { | |
$thumbnail_name = basename( $thumbnail_file ); | |
$thumbnail_file_path = $attachment_folder . '/' . $thumbnail_name; | |
// Check if the thumbnail has an original attachment | |
if ( preg_match( '/-\d+x\d+(\.[a-zA-Z]+)$/', $thumbnail_name, $matches ) ) { | |
$extension = $matches[1]; | |
$original_name = preg_replace( '/-\d+x\d+(\.[a-zA-Z]+)$/', '$1', $thumbnail_name ); | |
$original_file_path = $attachment_folder . '/' . $original_name; | |
if ( file_exists( $original_file_path ) ) { | |
unlink($thumbnail_file_path); | |
$thumbnail_count++; | |
} | |
// echo '<pre>'; | |
// var_dump($extension); | |
// var_dump($original_name); | |
// var_dump($original_file_path); | |
// echo '</pre>'; | |
} | |
} | |
$total_thumbnails += $thumbnail_count; | |
} | |
echo '<div class="success">' . $total_thumbnails . ' thumbnails have been deleted.</div>'; | |
} else { | |
echo '<div class="error">Please select at least one attachment to delete.</div>'; | |
} | |
} | |
?> | |
<h2>Attachments List</h2> | |
<!-- Filter by attachment title --> | |
<form action="" method="get"> | |
<label for="attachment_title">Filter by Title:</label> | |
<input type="text" id="attachment_title" name="attachment_title" value="<?php echo isset($_GET['attachment_title']) ? $_GET['attachment_title'] : ''; ?>"> | |
<button type="submit">Filter</button> | |
</form> | |
<!-- Display attachments table --> | |
<form action="" method="post"> | |
<table> | |
<thead> | |
<tr> | |
<th><input type="checkbox" id="select-all"></th> | |
<th>ID</th> | |
<th>Title</th> | |
<th>Thumbnail Count</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($attachments as $attachment) : | |
// $attachment_path = get_attached_file($attachment->ID); | |
// echo $attachment_path; | |
$attachment_meta = wp_get_attachment_metadata($attachment->ID); | |
$thumbnail_count = 0; | |
echo '<pre>'; | |
// var_dump($attachment_meta); | |
echo '</pre>'; | |
if (isset($attachment_meta['sizes'])) { | |
foreach ($attachment_meta['sizes'] as $size) { | |
$thumbnail_path = get_attached_file($attachment->ID); | |
$thumbnail_path = str_replace(basename($thumbnail_path), $size['file'], $thumbnail_path); | |
// echo '<pre>'; | |
// var_dump(basename($thumbnail_path)); | |
// var_dump($size['file']); | |
// var_dump($thumbnail_path); | |
// echo '</pre>'; | |
$thumbnail_count++; | |
} | |
} | |
$total_thumbnails += $thumbnail_count; | |
?> | |
<tr> | |
<td><input type="checkbox" name="attachment[]" value="<?php echo $attachment->ID; ?>"></td> | |
<td><?php echo $attachment->ID; ?></td> | |
<td><?php echo $attachment->post_title; ?></td> | |
<td><?php echo $thumbnail_count; ?></td> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<p>Total Thumbnails: <?php echo $total_thumbnails; ?></p> | |
<!-- Pagination --> | |
<?php | |
$pages = ceil($total_attachments / $per_page); | |
if ($pages > 1) { | |
$current_page = max(1, $page); | |
echo '<div class="pagination">'; | |
echo '<span class="page-numbers current">' . $current_page . '</span>'; | |
for ($i = 1; $i <= $pages; $i++) { | |
if ($i != $current_page) { | |
echo '<a class="page-numbers" href="?page=' . $i; | |
if (isset($_GET['attachment_title'])) { | |
echo '&attachment_title=' . urlencode($_GET['attachment_title']); | |
} | |
echo '">' . $i . '</a>'; | |
} | |
} | |
echo '</div>'; | |
} | |
?> | |
<!-- Delete selected thumbnails button --> | |
<button type="submit" name="delete_thumbnails" onclick="return confirm('Are you sure you want to delete the selected thumbnails?');">Delete Selected Thumbnails</button> | |
<!-- Select all attachments checkbox --> | |
<script> | |
document.getElementById('select-all').addEventListener('change', function() { | |
var checkboxes = document.getElementsByName('attachment[]'); | |
for (var i = 0; i < checkboxes.length; i++) { | |
checkboxes[i].checked = this.checked; | |
} | |
}); | |
</script> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment