Skip Navigation

[Resolved] Delete existing attached image when uploading a new image in edit form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 1 voice.

Last updated by Rita 1 week, 5 days ago.

Assisted by: Minesh.

Author
Posts
#2836588

Hi there
I am hoping this is possible! I would like to add a function that automatically deletes the existing feature image when a new image is uploaded in an edit form. Effectively replacing the image instead of adding another one so that a page/post can only have one image attachment.
Thanks in advance.
Rita

#2836734

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

We do not encourage to do delete the existing uploaded image due to a logical reason behind that as it could be used by another post/page and if you can delete the image such posts/pages will be broken and image will not displayed.

#2836839

Hi Minesh
I really appreciate your caution! However this site doesn't use any image on more than one post and each post has only one image. My client uploads many images (sometimes 10 or more...) to a post before choosing an image to publish and we now have 1000's of posts with forgotten images sitting on the database doing absolutely nothing. My client is also very reluctant to go to the media library in the dashboard and delete these many unwanted and unused images manually and would love for that to happen programmatically. I have tried to see if we can have a delete option on images in the media library accessed via the form but unfortunately this seems to be impossible with Toolset as well (https://toolset.com/forums/topic/allow-users-to-delete-their-own-media-attachements-via-cred-form/).
If there is a function that can achieve this I would love to be able to implement it and help this client manage her enormous and largely redundant media library.
Look forward to hearing from you.
Rita

#2836938

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok I got it.

*** Please make a FULL BACKUP of your database and website.***

What if you try to use the following plugin and check if that helps:
- https://wordpress.org/plugins/media-cleaner/

#2837045

Yes, I am aware of several very good plugins (which I have used to 'cleanup') but the aim here is to prevent rather than fix...
Is there really no function to delete the existing feature image when a new image is uploaded in an edit form?

#2837219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you can use the Toolset form API hook "cred_before_save_data":
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

For example:

add_action('cred_save_data', 'func_check_and_delete_old_featured_image', 10, 2);
function func_check_and_delete_old_featured_image($form_data) {

    if ($form_data['id']==99999) {

       $post_id = $form_data['container_id'];
       $old_featured =  get_post_thumbnail_id($post_id);

    if ($old_featured) {
        wp_delete_attachment($old_featured, true); // delete permanently
    }

    
  }
}

Where:
- Replace 99999 with your original form ID
- You can adjust the code as required, this is just a sample code

#2837329

Thank you Minesh! That solves the problem.