Skip Navigation

[Resolved] Delete pictures permanently

This support ticket is created 5 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1237381

I'm curious to know if it's possible the following:

In the Post Edit Form, when the user decides to change the images (custom image field) by deleting the current pictures so they can upload the new ones, how can I make sure that the delete pictures are deleted permanently from the database and not just from the custom fields?

Similarly, when the user decides to delete his/her custom post, how can I make sure that the pictures attached to the custom post are also deleted permanently?

Thanks for the help.

#1237386

Both of these will require a significant amount of custom code, and unfortunately I don't have cut-and-paste solutions available that will do exactly what you're looking for. The Forms API offers some event hooks that might be helpful. For the Post Edit Form, you can use the cred_before_save_data hook to inspect the existing custom field values in a post, and also inspect the incoming updates. If the updated file name is empty or different, use the WordPress API to delete the existing image file(s) permanently using wp_delete_attachment:
https://codex.wordpress.org/Function_Reference/wp_delete_attachment

When deleting a post you can use the before_delete_post hook as described in the WordPress documentation here: https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
Within that callback you can use get_post_meta to inspect the values of any custom image fields in the post. Then you must somehow identify those attachment posts in the database by ID (this might be difficult if you're renaming image files upon upload), and delete those posts using wp_delete_attachment: https://codex.wordpress.org/Function_Reference/wp_delete_attachment

#1239185

My issue is resolved now. Thank you!