Skip Navigation

[Resolved] Question: How to rename a file from a 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 2 replies, has 2 voices.

Last updated by Minesh 3 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2714529

Hi,

I'm looking for ways to rename a file from a form, ideally early or locally in the browser, before interacting with the server

Is this possible? Maybe it would be a good option for the future, I can think of several applications for this (in my particular case it's to prevent a watermark from being applied to certain specific files)

Any advice or help is very welcome

Thanks, regards

#2714554

I think I was able to solve it, in my particular case I use WordPress Media Library. I show disconnected users another form (I've mentioned this in other tickets, I think it's very painful to force the user to upload one file at a time, I really think you guys should review this in the future, plus Toolset's promise is to be a plugin with little code and user-friendly)

But getting back to the topic of the ticket, since I'm using Media Library I can modify this via php on the server, it's something like this:

function add_prefix($file) {
if (isset($_POST['toolsetFormsMetaKey']) && $_POST['toolsetFormsMetaKey'] == 'wpcf-custom-field') {
$file['name'] = 'prefix_' . $file['name'];
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'add_prefix');

With this I'm fine, I've achieved what I was looking for, although there may be a more global solution via JS, so that the file that the server works with and stores already has the prefix or name modified

If there is a JS solution it would be interesting

Thanks, greetings

#2715508

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

No - there is no custom JS workaround available that I should share and as your are using media uploader it's better to use the hook to change the file name on fly.

Toolset also offers following hooks that you might checkout:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#2715900

Thanks for replying