Created
January 13, 2022 13:11
-
-
Save renrax/51d764d2a5a648fed168cf4b78bcf58d to your computer and use it in GitHub Desktop.
WooCommerce - Удалить изображения и галерею товара после его удаления
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
<?php | |
// Автоматическое удаление изображений Woocommerce после удаления продукта | |
// 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