1. FOR Remove Attached Image with Product in Woocommerce
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 );
}
}
}
2. FOR Remove Attached Image with Post And Page
add_action("before_delete_post","wdm_delete_post_images",10,1);
function wdm_delete_post_images($post_id)
{
global $wpdb;
$args = array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any'
);
$childrens = get_children( $args);
if($childrens):
foreach($childrens as $attachment):
wp_delete_attachment( $attachment->ID, true );
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = ".$attachment->ID);
wp_delete_post($attachment->ID,true );
endforeach;
endif;
}
loading...
No comments:
Post a Comment