Plugin Author
Bowo
(@qriouslad)
What are you using/doing to achieve the first one? i.e. delete images as you delete a post.
The second one has been requested as well, most likely will go into the Pro version. Please understand that the free version is already generous enough with 40+ modules.
For both i use a code from chat gpt.
1- code to delete images:
// Hook into post deletion
add_action('before_delete_post', 'delete_related_images_bulk');
function delete_related_images_bulk($post_id) {
static $all_attachment_ids = array(); // Store all attachment IDs
if (function_exists('is_product') && is_product($post_id)) {
// If it's a WooCommerce product, get the product gallery attachments
$product = wc_get_product($post_id);
$attachment_ids = $product->get_gallery_image_ids();
} else {
// For regular posts, get attached images including featured image
$attachment_ids = get_attached_media('image', $post_id);
$featured_image_id = get_post_thumbnail_id($post_id); // Get the featured image ID
if ($featured_image_id) {
$attachment_ids[] = $featured_image_id; // Add the featured image to the array
}
}
if (!empty($attachment_ids)) {
$all_attachment_ids = array_merge($all_attachment_ids, $attachment_ids);
}
// Final deletion action after all posts are processed
add_action('delete_post', function() use ($all_attachment_ids) {
foreach ($all_attachment_ids as $attachment_id) {
// Check if the attachment is used in other posts or products
$usage_count = count(get_posts(array('post_type' => array('post', 'product'), 'post_status' => 'any', 'meta_query' => array(array('key' => '_thumbnail_id', 'value' => $attachment_id)))));
if ($usage_count <= 1) { // If used only in the current post/product
wp_delete_attachment($attachment_id, true); // Delete the attachment
}
}
});
}
Plugin Author
Bowo
(@qriouslad)
Thanks for the code snippets. Great that it also has a check to see if an image is used in another post or not.
Yes, hope to see it in your plugin 🌹🙏