Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchellkrogza/ac9f4f1ae79b56ec59bc62d7753f89f3 to your computer and use it in GitHub Desktop.
Save mitchellkrogza/ac9f4f1ae79b56ec59bc62d7753f89f3 to your computer and use it in GitHub Desktop.
Delete Woocommerce Product images when 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