When Michael Jenson needed help customizing the name of image uploads through a Formidable Pro form, Stephanie Wells of Strategy 11, the author of Formidable Pro, recommended that he contact us for assistance. We do a lot of custom Formidable Pro work and know the product well. Michael's form is located at http://cameraclub.photos/exhibition-upload/. His request:
What I’m looking for is to take all of the fields from this form (except email address) and use them to rename a jpeg image file upon upload. I have several clients who are camera clubs and they are requesting this functionality.
This is a screen print of Michael's form:
To clarify the requirement, I responded:
So let’s say I upload an image in category aa, you want the image name to be: victor-font-aa-imagename-open-digital.jpg. Is that correct?
Michael confirmed his requirement and shortly thereafter, I sent the following code to him for testing with instructions to copy it into his child theme's functions.php file.
rename_imagehttps://gist.github.com/vfontjr/199632e7e3d1447c4aeb0510e1b25dec#file-rename_image-phpMichael's response:
This worked awesome!
This Can Work With Most Any Form
The beauty of this code is that with a little modification it could work with any form that uses the WordPress upload process as Formidable Pro does. You would have to determine how your form tool identifies its forms and the naming convention for the fields. The code intercepts and sanitizes the filename just before WordPress saves it. If you need help, you can always reach out to us.
To learn more about the sanitize_file_name filter visit: https://codex.wordpress.org/Function_Reference/sanitize_file_name
Hi Victor! I’m using your beautiful snippet, after update v2.02 this snippet not worked because the entry values aren’t available via POST $_POST[‘item_meta’][x] :(
Thanks Mike. The team at Strategy 11 has been making so many improvements to Formidable Pro lately that it’s hard to keep up. I’ll look into it figure out how to manage this for the new version.
Did you happen to figure out the “fix” for this?
I have not yet, but I’ve been working with the new upload process on a client’s development site and I understand what it is doing. It does not upload the images to the WordPress media library. In fact, the new process stores uploaded images in a upload/formidable/x/ where x = form id.
Formidable Pro stores form related items following a data model similar to how WordPress stores posts. That is, all form related items are stored in two tables, frm_items and frm_item_metas. Frm_items stores basic header information for a record and frm_item_metas stores all the related field data. When I looked up the content in the database for a registration’s form profile photo, I was expecting to find the image url, but did not. All I found was a number. The number is the id of a standard WordPress attachment where you can its content in wp_posts and wp_postmeta.
With all that being said, unless there’s a REST endpoint that allows a post to be edited, or another way to intercept the upload before all the writing to the back end takes place, the process will have to be relegated until after the form is saved. There are 5 places the new file name would need to be applied if we were to follow this through to completion:
This requires quite a bit of work and I’m not sure when I’ll be able to tackle this given my current work load. All I can say is that I know how to do this, I just don’t know when.
Hi Victor,
thank you for sharing this!
How should it look like if upload of multiple files is allowed?
Best regards,
Stefan
Hi Stefan,
I don’t think it requires any changes for multiple file uploads. This is because we’re filtering the WordPress sanitize_file_name function, which executes for every uploaded file. It should just work as it is.