Crocoblock/jetformbuilder

Delete attachments from library

TheBigBanik opened this issue · 3 comments

Hi, I have a feature request. If a form contains media fields (preset enabled) we can see the current post's media files. If we delete a media file or update it with a new one and resubmit the form, the old attachments should be deleted from the library too. Imagine creating a realtor listing website that users can upload 10-20 images per post. And then they take new pictures of the property and re-upload the new ones. All old attachments will still exist in the database and so in the media library. But, they should be deleted.

I second this!!! this function as thebigbanik suggest is an absolute must have its actually a common sense approach. right now if you replace an image in "media field" all it does it upload another pic and change the meta and then the old file is now left behind on the server with no way to distinguish it as not in use. if you click delete in the "media field" all it does is clear the meta but it does not actually delete the image . this currently is absolutely irresponsible and will lead to server with lots of unused images piling up. one of my sites has 775K active users I had to install meta box and use that for the media upload because it has a slider option that says to "delete media from library on meta delete" i would rather be using jetforms to handle media uploads form, I know you have the dev plugin attach media to post which does work for making media delete but it only works for a cpt and nothing else like a cct or if i add a user field like profile_picture this also leaves behind a lot of empty junk folders in wp-content/uploads/jet-form-builder/ as when people upload media they get a folder created to each user but if the images are deleted the folders still very oddly remain behind on the server with a random empty index.html file inside it. bottom line the media field needs to ensure there is a way to make it delete images from server/media library instead of just clearing the meta and leaving the old media file just sitting now unused on the server

Hey @HostingPros, I found a workaround until Crocoblock team implement this feature.
Use a call hook like

add_action('jet-form-builder/action/after-post-update', function($action, $handler) {
$post_id = $handler->get_inserted_post_id($action->_id);

// Schedule the deletion task 5 seconds after post update ( I couldn't make it work instantly)
wp_schedule_single_event(time() + 5, 'delete_unused_attachments', array($post_id));

}, 10, 2);

// Handle the background task for deleting unused attachments
add_action('delete_unused_attachments', function($post_id) {
// Get all attachments for the current post
$attachments = get_posts([
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post_id,
]);
$attachment_ids = wp_list_pluck($attachments, 'ID');

// Get attachments from meta boxes
//Write a simple function to get all your attachment IDs from your meta boxes and add them in the variable
 $meta_attachments = [];
 
 //Lastly, delete only attachments from the post that don't exist in any meta box
 
     // Find the difference: delete attachments not in the meta
$attachments_diff = array_diff($attachment_ids, $meta_attachments);

// Delete the differences (attachments not in meta)
if (!empty($attachments_diff)) {
    foreach ($attachments_diff as $attachment_id) {
        wp_delete_attachment($attachment_id, true);
    }
}

It works perfect on my side, I am not sure though if the 5sec delay will be enough, I am not sure what is the time difference between form submission and post update, but you can try it.

This is a necessity, not an enhancement. 100+ please @Crocoblock.

This entire Media Field needs a facelift to bring it up to 2024. Possibly hook it up with Dropzone or have your own drag & drop.