Created
June 10, 2021 12:35
-
-
Save mitchellkrogza/ac9f4f1ae79b56ec59bc62d7753f89f3 to your computer and use it in GitHub Desktop.
Delete Woocommerce Product images when deleting a Product
This file contains 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
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