Quick question about image deletion
-
Hi there,
Just wanted to check.
If I delete an image from my media library, are the corresponding webp versions of the image created by Converter for Media also deleted?
I realise they are generated in a seperate folder, and wanted to double check.
If it does, is it fair to assume that using the following snippet to remove product images when you delete a product will also automatically work with the webp duplicates?
// Automatically Delete Woocommerce Images After Deleting a Product add_action( 'before_delete_post', 'delete_product_images', 10, 1 ); function delete_product_images( $post_id ) { // Check if user has the capability to delete products if ( !current_user_can( 'delete_products' ) ) { return; } $product = wc_get_product( $post_id ); if ( !$product ) { return; } $featured_image_id = $product->get_image_id(); $image_galleries_id = $product->get_gallery_image_ids(); if( !empty( $featured_image_id ) ) { $is_featured_image_used = is_image_used( $featured_image_id, $post_id ); if ( !$is_featured_image_used ) { wp_delete_attachment( $featured_image_id, true ); } } if( !empty( $image_galleries_id ) ) { foreach( $image_galleries_id as $single_image_id ) { $is_image_used = is_image_used( $single_image_id, $post_id ); if ( !$is_image_used ) { wp_delete_attachment( $single_image_id, true ); } } } } function is_image_used( $image_id, $current_product_id ) { $query = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_thumbnail_id', 'value' => $image_id, 'compare' => '=' ), array( 'key' => '_product_image_gallery', 'value' => '"'.$image_id.'"', 'compare' => 'LIKE' ) ), 'post__not_in' => array( $current_product_id ), 'fields' => 'ids', 'posts_per_page' => -1 ) ); return ( $query->have_posts() ); }
thank you for your help!
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- You must be logged in to reply to this topic.