Skip Navigation

[Résolu] Delete images from server after front-end CRED Form update

This support ticket is created Il y a 4 années et 7 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Ce sujet contient 7 réponses, a 2 voix.

Dernière mise à jour par Luo Yang Il y a 4 années et 7 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1601273

Hi guys,

I know that this has been discussed before, and I got your logic about this being as expected and possibility of photos being used elsewhere, but please listen to me very carefully, because I can not express myself any clearer..

I NEED To cleanup the server from images that people uploaded to their profile(CPT), but then deleted via the front-end form, in order TO GROW MY PROJECTS!

Leaving those images on the server is unthinkable, because this means that I will have to keep adding storage capacity to my web server forever simply because my users use the platforms, even when the user base stops growing.

SO PLEASE, help me get this going as this is the only thing that prevents my projects of having hundreds of thousands of users. I can not even start advertising them, knowing that without getting file management right from the start I’m doomed to have messed up server that I will never be able to clean up. And as I see I’m not the only one having this issue.

Once again - I don’t care about WP logic, or photos being used elsewhere, because they are not!

- I have each user photos stored in their own folder (it has their ID as a name).
- I have limited users access to their own uploads only.
- And we’re talking about user profile(CPT) photos (and a repeating field as part if a gallery), so photos being used elsewhere is simply not true.

I want a fix for this *HUGE BUG* on front-end forms now, and I don’t care about if you see it as a bug or not, because I’m the client here, and I say that it is a bug, and a PETABYTE LEAK.

Please finally! Help me solve this issue and let me grow my all my projects now!

#1602353

Hello,

Thanks for the details, there isn't such kind of built-in feature within Toolset Forms plugin, as a workaround, you can try with custom codes, for example:
1) When user submit the post form, use action hook cred_save_data to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/

2) In this PHP function:
- Get the images attached to current post,
https://developer.wordpress.org/reference/functions/get_attached_media/

- Get the images filled in your custom repeating image field
https://developer.wordpress.org/reference/functions/get_post_meta/

- Compare above two values, and find those unused image files, and remove them:
https://codex.wordpress.org/Function_Reference/wp_delete_attachment

#1602365

Hi Luo,

Thanks for your input on my issue.

I will test this. I will possibly have to also check for one photo being used as a profile photo, as this is a separate custom field.

Once ready, I will report back and share my code so that other users can use it.

D.

#1603407

OK, please let me know if you need more assistance for it, thanks

#1604187

Okay guys, this is for all the other Toolset users who waited for this function!

/* 10 Custom Action to Cleanup files from database and server upon submitting specific cred forms */
add_action('cred_save_data','images_cleanup', 10, 2);
function images_cleanup($post_id, $form_data) {
	# if a specific form or two..
	if (($form_data['id']==88) || ($form_data['id']==114)) {
	    $type = get_post_type($post_id, $form_data);
	    # if a specific post type..
	    if ($type == 'profile') {
	     	global $wpdb;
			# Compare both tables and remove duplicates while listing only the ID from wp_post as attachment
			$allposts = $wpdb->get_results( $wpdb->prepare("SELECT id FROM wp_posts WHERE post_parent=$post_id AND post_type ='attachment' AND guid NOT IN (SELECT meta_value FROM wp_postmeta WHERE post_id = $post_id AND meta_key LIKE 'wpcf-user-gallery' OR meta_key LIKE '_wp_attached_file' OR meta_key LIKE 'wpcf-user-photo')") );
			foreach ($allposts as $singlepost) { 
				$post_id_n1=$singlepost->id;
				wp_delete_attachment($post_id_n1, true);
			} # END global $wpdb;
	    } # END specific post type
	} # END specific form
  } # END function images_cleanup 

Make sure you update your form ID's (two forms because one is to Create the CPT, and the other is to Edit it) and your post type (mine is 'profile' here)!

You might also want to change your file structure in the uploads folder to have a separate folder with the user's ID, use this code:

/* Per user upload dir function */
	function per_user_upload_dir( $original ){
	    // use the original array for initial setup
	    $modified = $original;
	    // set our own replacements
	    if ( is_user_logged_in() ) {
	        $current_user = wp_get_current_user();
	        $subdir = $current_user->ID;
	        $modified['subdir'] = $subdir;
	        $modified['url'] = $original['baseurl'] . '/' . $subdir;
	        $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
	    }
	    return $modified;
	}
	add_filter( 'upload_dir', 'per_user_upload_dir');

Enjoy!

To Toolset developers:
- Please consider the file attachment cleanup option as an addition to your plugin functionality. Otherwise websites will become server hogs..

#1605169

Thanks for sharing the codes, that will help other users.

And I will forward your request for our developers evaluation.

#1605241

My issue is resolved now. Thank you!

#1605255

Sorry for the typo, just corrected