Home › Forums › Add-ons › Gallery Field › Deleting images from media library › Reply To: Deleting images from media library
Something like
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_field($field['name'], $post_id, false);
if (!is_array($old_value)) {
return $value;
}
if (!$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;
}
but you should be sure that only images that are uploaded to posts can be added to galleries.
Also, when someone is editing content and creates a standard WP gallery, as well as adding images to content and and then having them deleted by one of these functions you’ll get errors in other areas of your site. Hopefully the person that gave you that code warned you about this.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.