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