March 23, 2022: Fixed issue where editing entries belonging to forms with multiple
GW_Rename_Uploaded_Files
on the same form would cause files to be re-renamed.March 12, 2021: Added
ignore_extension
option.June 12, 2020: Improved support for non-latin characters.
Jun 27, 2018: Updated to use
get_upload_dir()
method instead ofwp_upload_dir()
.November 19th, 2016: Fixed typo in method name.
October 24, 2016: Fixed issue with Gravity Forms 2.0.7. Major refactor. Requires Gravity Forms 2.0.7.15 or greater.
December 5, 2015: Added support for Post Image fields.
September 22, 2015: Updated to remove forward and backslashes from new filename.
March 26, 2015: Fixed issue when submitting form with no uploaded files
Stop! There's a better way.
This snippet is available as a plugin with Gravity Perks, a suite of over 47 premium Gravity Forms plugins!
We wrote a snippet that will allow you to create a dynamic naming template for your uploaded Gravity Form files. This includes the ability to prepend, append or generate from scratch custom file names. You can use merge tags to include user input in your new file names.
Here are a couple use cases where this snippet might be useful:
- Include the name of the submitting user either before or after the file name.
- Include an event name in the file name to better categorize which files belong to which events.
For example, given an original file name of mountains.png and a template of {Name (First):1.3}-{Name (Last):1.6}-{filename}
, the final image name would be Jordan-Smith-mountains.png
Getting Started
- Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
- Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file.
- Configure the snippet
- Once you have installed the snippet, find the snippet configuration section located at the bottom of the snippet code.
- Replace the
form_id
with the ID of your form. - Replace the
field_id
with the field ID of your File Upload field. - Replace the
template
with your custom file name template.
Usage Examples
First and Last Name
new GW_Rename_Uploaded_Files( array(
'form_id' => 628,
'field_id' => 3,
'template' => '{Name (First):1.3}-{Name (Last):1.6}-{filename}', // most merge tags are supported, original file extension is preserved
) );
In this example, we are prepending the first and last name from the form to easily identify who submitted the file. Most merge tags are supported using this method and the original file extension is preserved.
Form Title Merge Tag
new GW_Rename_Uploaded_Files( array(
'form_id' => 12,
'field_id' => 18,
'template' => '{form_title}-{filename}', // most merge tags are supported, original file extension is preserved
) );
In this example, we are prepending the title of the form to the filename to quickly identify which form the file was uploaded to.
Static Text
new GW_Rename_Uploaded_Files( array(
'form_id' => 628,
'field_id' => 5,
'template' => 'static-file-name',
) );
You can use any text you’d like, just make sure to add the {filename}
merge tag somewhere in there.
Ignore Extension
new GW_Rename_Uploaded_Files( array(
'form_id' => 628,
'field_id' => 5,
'template' => 'static-file-name',
'ignore_extension' => true,
) );
By default, this snippet takes the extension into consideration when renaming files. For example, static-file-name.jpg
is different from static-file-name.png
. You can change this behavior to ignore the extension of a file so in this scenario static-file-name.png
would be renamed to static-file-name1.png
as there’s already a static-file-name
file uploaded.
Parameters
form_id (integer) (required)
Your Form ID. There is no default value.
field_id (integer) (required)
The Field ID of the file upload field. There is no default value.
template (string or merge tag) (required)
This is the template that controls how the file will be rewritten. The value can be either a static string or a merge-tag(s). There is no default value.
Was this helpful?
We hope you find this snippet useful! If you do, show us some love by sharing this article on the social media platform of your choice. Let’s keep the Gravity Wiz pumping out awesome snippets. Thanks!
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
Bonjour Merci pour cette explication et ce code qui fonctionne parfaitement ! Il renomme bien mes fichiers qui sont hébergés sur mon site par contre lors de la validation de mon formulaire gravity form en ligne une notification part vers les intéressés en joignant un fichier zip avec tous les fichiers ! Là mon problème c’est que ces fichiers ne sont malheureusement renommés ! connaissez vous une solution à ce problème ? Il faudrait que la modification du nom se fasse dès que la personne joint sa pièce jointe au formulaire et pas au moment de l’envoie je pense ! merci par avance
Hi Anthony,
I’m not able to reproduce this issue when I test locally. If you’re a Gravity Perks customer, we’re happy to dig into this further. Drop us a line.
Hi- Thanks for the snippet! The one issue I’m having is that the {filename} merge tag you reference in the examples doesn’t carry over to the new filename, like in the ‘mountains.png’ example above.
Filename prior to upload: aJ19p_So.pdf
new GW_Rename_Uploaded_Files( array( ‘form_id’ => 2, ‘field_id’ => 32, // most merge tags are supported, original file extension is preserved ‘template’ => ‘2_{filename}’, ) );
Resulting filename: 2_filename.pdf
I feel like I’m missing something really obvious here…
Thanks! Spencer
Hi Spencer,
You aren’t missing anything. I can confirm the issue with the {filename} merge tag. I’m forwarding this to our developers to look into this. Please expect an update to this comment when this is fixed.
Best,
How can I programmatically populate a hidden field value (based on values submitted on other fields), in a way that the hidden field will be populated and used in the rename template?
i.e. Field One (ID:1) – user will submit a value Field Two (ID:2, hidden) – my code will calculate this value based on the user submission
‘template’ => ‘{Field Two:2} xyz’
I have tried using different actions and filters with different priority paramters, but to no avail…. please help.
Hi Ofra,
You can use our GP Populate Anything Perk to populate the Hidden field based on the value in the other field. I will also suggest using our GP File Renamer Perk to rename the files instead of using this snippet.
Best,
Hello,
I’m using WPCode and it works when I only put two parts of configurations.
new GW_Rename_Uploaded_Files( array( ‘form_id’ => 1, ‘field_id’ => 16, // most merge tags are supported, original file extension is preserved ‘template’ => ‘{entry_id}/{filename}’, // Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.) ‘ignore_extension’ => false, ) );
new GW_Rename_Uploaded_Files( array( ‘form_id’ => 1, ‘field_id’ => 22, // most merge tags are supported, original file extension is preserved ‘template’ => ‘{entry_id}/{filename}’, // Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.) ‘ignore_extension’ => false, ) );
But when I put all my code into the snippet, it will show “We encountered an error activating your snippet, please check the syntax and try again.” “Snippet not activated, the following error was encountered: syntax error, unexpected ‘ ‘ (T_STRING), expecting ‘)'”
I’m not sure is there any problem with my code or it’s the plugin’s problem?
The complete code is show in the link below
https://codeshare.io/lorlmB
Hi,
The complete code snippet is correct and shouldn’t cause any error message. The error message you’re receiving shows that you’re missing a closing brace. Can you copy and paste the code again and see if it helps?
Best,
Hi Samuel,
I copied and pasted it again, the error message is still the same @@
Hi
Can you try this code and see if it works for you?
Best,
Hi Samuel ~
Yes, it works now!
But I cannot find the differences in the two codes
So maybe I shoud edit the code in notepad and paste it into WPCode
Thanks for your help again
You’re welcome. Yes, there is no difference between the two codes. However, when I pasted the code into a code editor, it showed some unwanted characters that I had to remove to get it working.
Best,
Thanks for a great plugin. I would like the image the user uploads to be named after the role it is a member of and saved to a folder.
It should be saved in /wp-content/uploads/company/ and named rolename.jpg . Is this possible?
I’m using ultimate member, so I’ve tried several different ways like these shortcodes: ‘template’ => ‘{um_display_role}’, ‘template’ => ‘[um_display_role]’, But then only the file name will be um_display_role.jpg.
If the role is named company, then I want the file to be saved as company.jpg . If the file already exists, I want it to be overwritten and of course only saved in /wp-content/uploads/company/ .
Can you help me with that?
Hi Robin,
The {um_display_role} shortcode wouldn’t work when used as the value for the template parameter. What you can do is to dynamically populate a hidden field on the form with the role of the user and then use the merge tag of the hidden field as the value for the template parameter to rename the file with the user role.
Best,
When I add the snippet through code snippets plugin it is showing the following error: The code snippet you are trying to save produced a fatal error on line 1:
Uncaught Error: Class ‘GW_Rename_Uploaded_Files’ not found in /home/XXXX…..
Did I miss something?
Hi Omar!
Thanks for your comment. We are sorry you are facing this issue with our snippet.
We would like to take a look at your snippet added through the Code Snippet plugin. If you have a Gravity Perks license, can you get in touch with us via our support form?
Hello, how to avoid changing the name with a number in the same extension, or rather how to rename again and again the same file with the same extension.
Example: Want to avoid this: a.jpg, a1.jpg, a2.jpg.
You want to achieve this: a.jpg, a.jpg, a.jpg, a.jpg
Hi Arden,
It seems this is not currently supported and it will require some customization. If you have a Gravity Perks license, can you get in touch with us via our support form?
Cheers,
Does this work with the Dropbox Upload extension?
Also, does it work when ‘multiple uploads’ is enabled?
Hi Todd,
It should work when multiple uploads are enabled. About the dropbox add on integration tou can get in touch with us via our support form if you have an active Gravity Perks license, so we can take a look at your setup and assist you to set it up.
Hi this snippet looks great how to rename 2 or more files in the same form
Hi Jasim,
To rename two or more files on the same form, you have to duplicate the configuration and update the IDs accordingly. Here is an article with details on how to do this.
Best,
Hi
Thank you for the plugin
Ive Installed the plugin and just just wanted to find out
How do i add white space or brackets?
Thank you
Hi Muhammed,
You can’t have a white space in a filename, because WordPress would replace it with a dash and it will also sanitize any special character including brackets by removing them.
Best,
Hello. This is a great snippet, thanks so much for making it available. I do have one issue, though:
I am prepending unique identifiers to file names in files uploaded by customers. That part is working great. However, administrators utilize GravityView to access the same form wherein they will occasionally make an edit to other entries. When that happens, even if it’s an edit to a totally unrelated entry, the renamed files get renamed all over again. For example, a file may start out as IMG1000.jpg, it gets uploaded and renamed to unique_identifier_IMG1000.jpg, but then it gets renamed yet again to unique_identifier_unique_identifier_IMG1000.jpg if the administrator edits any entry in the form. Is there a way to prevent this renaming duplication from happening? Thanks in advance for your help.
Hi,
I am unable to recreate this issue so I have sent you an email to request more information. Please check and reply.
Best,
Doesn’t work.
Error thrown Class ‘GW_Rename_Uploaded_Files’ not found
Hi D,
Based on the description of the error. It seems the snippet was not installed. How do I install a snippet?.
If you have any further questions and you have an active license, you can get in touch with us via our support form.
Cheers,
Hi, I have more than 20 forms and each of them allows to upload up to 5 images, is it possible to make the code dynamic so that it works throughout the site? Or are there other solutions (apart from having to maintain 100 different snippets).
Thanks, Nicola
Hi Nicola,
If you want to use the file rename snippet on more than one file upload field and on multiple forms, you only have to set up multiple configurations. You don’t have to duplicate the entire snippet, but just the configuration part which will create multiple instances of the Class. Here is an article on how to apply a class-based snippet multiple times.
I hope this helps.
Best,
Can anyone think of a way i can get the category slug instead of the category name? This is for a contest entry, so to make a random number, I’m grabbing some mixed up time stamp. ($result = $date->format(‘uYHmids’);)
‘template’ => ‘{Category:9}-‘.$result,
Field 9 on my form is the Post field Category, but i’d prefer to pull in the slug instead Could there be a hidden field on the form maybe that i could use to populate with the category slug once the user fills in the Category field?
Or any other thoughts?
J A
Hi there,
You could populate a hidden field with the category slug using Populate Anything. To do this, you need to filter the category based on the selection made in the Post Category field. If you have questions, feel free to reach out to support.
as per initial comment – how do we use on more than one form? is that possible – appears you can set multiple configurations but not sure if that is just that part of the code or does all of the code require duplication with appropriate settings – got it to work with form A) now want to also use for form B) – thanks in advance – cool snippet (appreciate the share)
Hi Steve,
To use the snippet on more than one form, you only have to set up multiple configurations. You don’t have to duplicate the entire code. You can read more about here.
Question: How do I modify this to include multiple instances. I have one form with 3 different file upload fields. Need different naming conventions for each field.
Hi,
We’ve already followed up via email. If you have more than one field per form, you will need to create as many configurations as fields you have.
Best,
I have a question
When I add the code in function.php file, it’s work correctly and rename any file i Upload it
but it’s work only when i create new gravity form with file upload
old forms i was created before add the code it’s not work its rename a file like name-first1-3.jpg
how can i make the code work with old form i have ?
Hi Mohamed,
That’s strange because the snippet should work on old forms without any issue. You may want to check the configuration to see if you’re passing the right parameters. In case it’s still not working, you can get in touch with us via our support form if you have an active Gravity Perks license, so we can dig into this further.
Best,