Delete images from media library when user deletes an image from ACF Gallery
When a user deletes an image from the gallery in wp-admin I need it to be deleted from the media library too. I have a function using the acf/update_value
hook which works uncorrectly. When a user simply updates a post, WordPress removes all images from the media library and current gallery field. Please tell me how to fix it? All the old questions don't work :(
add_filter('acf/update_value/type=gallery', 'remove_gallery_images', 10, 3);
function remove_gallery_images($value, $post_id, $field) {
$images_to_delete = array();
$old_value = get_post_meta($post_id, $field['name'], true);
$old_value = maybe_unserialize($old_value);
if (!is_array($old_value)) {
return $value;
}
if (!empty($value)) {
$images_to_delete = $old_value;
} else {
foreach ($old_value as $image_id) {
if (!in_array($image_id, $value)) {
$images_to_delete[] = $image_id;
}
}
}
if (count($images_to_delete)) {
foreach ($images_to_delete as $image_id) {
wp_delete_attachment($image_id, true);
}
}
return $value;
}
Topic advanced-custom-fields functions php gallery custom-field Wordpress
Category Web