Created
November 29, 2024 08:24
-
-
Save kkumar326/a15ae7de13818cdb849356533ba01a47 to your computer and use it in GitHub Desktop.
Delete woocommerce images on product deletion (does not include generated images like webp)
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
// automatically delete Woocommerce Images after deleting a Product | |
add_action( 'before_delete_post', 'delete_product_images', 10, 1 ); | |
function delete_product_images( $post_id ) | |
{ | |
$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 ) ) { | |
wp_delete_post( $featured_image_id ); | |
} | |
if( !empty( $image_galleries_id ) ) { | |
foreach( $image_galleries_id as $single_image_id ) { | |
wp_delete_post( $single_image_id ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment