Skip to main content

Formidable Forms: File Upload Folder

Changing the File Upload Folder used with the Formidable Forms File Upload element can be achieved quite easily using the frm_upload_folder hook.

The fine folk at Formidable Forms have provided 3 handy examples to get you started HERE. These examples are great if you want to change the file upload folder to a fixed location or organise the uploaded files by username. But what if you want to do something a little more dynamic?

We came across this issue when developing a new solution for our existing clients as we have a File Upload Folder per client created in a sub-directory of our site and we needed to target these existing folders for each logged in user.

To further complicate matters, our File Upload Folders were created and controlled by another WordPress Plugin and a bespoke field in each user profile stored in the database.

So how do we get Formidable Forms to target a dynamic File Upload Folder for each logged in user?

The Solution:

The solution is actually nice and simple although it can get a bit tricky if you’re not familiar with WordPress Plugins or MySQL. The function below targets a specific field folder location and looks up the logged in users own file upload folder from their user profile to set a bespoke File Upload Folder for each logged in user.

Note: This solution assumes a few things. Firstly that you already have these folders created and secondly that you have a filed in each users profile with that folder name present.

add_filter( 'frm_upload_folder', 'frm_custom_upload', 10, 2 );
function frm_custom_upload( $folder, $atts ) {
if ( $atts['form_id'] == 92 ) { // change to your form id
global $current_user;
$folder = '../uploads/wpclient/_file_sharing/' . $current_user->wpc_cf_client_file_cat;
}
return $folder;
}

Setting A New File Upload Folder

Copy and paste the snippet above into a Custom WordPress Plugin (there’s a really good article on how to do this HERE), your functions.php file or into a plugin like the Code Snippets plugin and adjust as required.

To begin with we need to set the main part of the folder location. In our case this is /uploads/wpclient/_file_sharing/

The folder file path requires ../ at the start to take you up a level from the default formidable folder. You may need to add multiple copies of this to take you ‘up’ more than one level. From there you can define the folder you wish your files to be uploaded to.

As we have a custom field in each user profile, which contains the name of the folder we’re looking for, we need to look this up and add it to the end of the file path.

We do this using: $current_user->wpc_cf_client_file_cat

$current_user is the function to get the currently logged in user and wpc_cf_client_file_cat is the filed in the database that contains our custom folder name.

So now when my client Mr Test logs in and uploads a file using this Formidable Form it will be uploaded to his existing folder like so:

/public_html/wp-content/uploads/wpclient/_file_sharing/MrTest instead of the normal Formidable Forms directory.

And thats it!

We hope you found this tutorial useful. Check out the other Formidable tutorials in our blog or leave a comment below.