Skip Navigation

[Resolved] Rrename and move files after upload and prevent adding to media library

This thread is resolved. Here is a description of the problem and solution.

Problem:
I want to upload multiple files (one repeating-field) via CRED-form.

After successfull uploads, i want to move and rename the files to a different file-directory (with a little bit php-code via the cred_submit_complete hook). Don´t want to use hooks like "wp_handle_upload_prefilter", "upload_dir", or "cred_form_ajax_upload_validate". After that i have to update the custom repeating field with the new renamed file-paths and file-names.

Unfortunatelly the upload-function by toolset will add the files to the media library automatically and i don´t want that.

So my two questions are

1. Is the hook the right place for renaming, moving and updating uploaded files?
2. How do i prefent to attach files to the media library?

Solution:
Q1) Is the hook the right place for renaming, moving and updating uploaded files?
Yes, I think it is the right place. And it is possible to use action hook "cred_submit_complete" to achieve what you want, for example when user submit the CRED form successfully, use action hook "cred_submit_complete" to trigger a custom PHP function, in this PHP function, get the file/image field value:
https://developer.wordpress.org/reference/functions/get_post_meta/
Then rename the file name to what you want:
http://php.net/manual/en/function.rename.php

Q2) How do i prevent to attach files to the media library?
You can simply remove it from medial library:
https://developer.wordpress.org/reference/functions/wp_delete_post/

For example, use action hook "cred_submit_complete" to trigger a custom PHP function, in this PHP function, get the file/image field value
https://developer.wordpress.org/reference/functions/get_post_meta/
get the post ID of file/image in media library:
https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
Then remove it from media library:
https://developer.wordpress.org/reference/functions/wp_delete_post/

For your reference.

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_delete_post/
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 6 years, 5 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
- 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)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#592867

I want to upload multiple files (one repeating-field) via CRED-form.

After successfull uploads, i want to move and rename the files to a different file-directory (with a little bit php-code via the cred_submit_complete hook). Don´t want to use hooks like "wp_handle_upload_prefilter", "upload_dir", or "cred_form_ajax_upload_validate". After that i have to update the custom repeating field with the new renamed file-paths and file-names.

Unfortunatelly the upload-function by toolset will add the files to the media library automatically and i don´t want that.

So my two questions are

1. Is the hook the right place for renaming, moving and updating uploaded files?
2. How do i prefent to attach files to the media library?

Thank you so much for an hint.

#593259

Dear Mario,

Q1) Is the hook the right place for renaming, moving and updating uploaded files?
Yes, I think it is the right place. And it is possible to use action hook "cred_submit_complete" to achieve what you want, for example when user submit the CRED form successfully, use action hook "cred_submit_complete" to trigger a custom PHP function, in this PHP function, get the file/image field value:
https://developer.wordpress.org/reference/functions/get_post_meta/
Then rename the file name to what you want:
hidden link

Q2) How do i prevent to attach files to the media library?
You can simply remove it from medial library:
https://developer.wordpress.org/reference/functions/wp_delete_post/

For example, use action hook "cred_submit_complete" to trigger a custom PHP function, in this PHP function, get the file/image field value
https://developer.wordpress.org/reference/functions/get_post_meta/
get the post ID of file/image in media library:
hidden link
Then remove it from media library:
https://developer.wordpress.org/reference/functions/wp_delete_post/

For your reference.

#593976

Thank you so much Luo. Great support - thumbs up.

#594192

You are welcome

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.