Are you annoyed that Visual Form Builder Pro places your file uploads mixed in with the uploads for your Posts and Pages? If so, this bit of code is for you.
Visual Form Builder Pro uses a WordPress function called wp_handle_upload to handle uploading and moving the file to the appropriate content directory. The upside (or downside, depending on how you look at it) is that is handles everything you need in a single function. You just need to pass it the $_FILES reference. With this simplification comes a price and the reason for this tutorial: customizing where the files goes is handled outside this function.
upload_dir filter
The upload_dir filter takes an array of all the path and URL info it needs to upload a file. For our purposes, though, we only want to affect uploads that happen through Visual Form Builder Pro and not every upload. This is done by checking to see if our file upload field is set and, if so, change the upload directory. Otherwise, leave as is and continue as normal.
add_filter( 'upload_dir', 'vfb_upload_dir' );
function vfb_upload_dir( $upload ){
/* If our File Upload field is detected.
To change all forms, use $_POST['visual-form-builder-submit']
*/
if ( isset( $_FILES['vfb-file-upload-611'] ) ) {
/* Customize this to fit your needs */
$dir = WP_CONTENT_DIR . '/vfb-uploads';
$url = WP_CONTENT_URL . '/vfb-uploads';
$bdir = $dir;
$burl = $url;
$subdir = '';
/* Append year/month folders if that option is set */
if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
if ( !$time )
$time = current_time( 'mysql' );
$y = substr( $time, 0, 4 );
$m = substr( $time, 5, 2 );
$subdir = "/$y/$m";
}
$dir .= $subdir;
$url .= $subdir;
/* Set the $uploads array with our new paths */
$upload['path'] = $dir;
$upload['url'] = $url;
$upload['subdir'] = $subdir;
$upload['basedir'] = $bdir;
$upload['baseurl'] = $burl;
$upload['error'] = false;
}
return $upload;
}
This function can be customized to fit your needs as you see fit. This particular function will only run for the form that has the ‘vfb-file-upload-611′ field, but you can expand it to include or exclude more forms. You can even change it to pick up on all VFB forms if that’s what you want to do.
IMPORTANT NOTE!
Changing the upload directory in this way will affect how these uploads work in the Media Library. In other words, you won’t see the thumbnail and the links in the Media Library will be pointing to the wrong file location. WordPress stores the file location attachment metadata as a relative path, so it’s always going to be in reference to your default upload directory.
If customizing the upload path is more important than seeing thumbnails in the Media Library, then this detail can be overlooked.
Hi,
could i know, why i cant get email notification when I’ve filled up the form,
but after I’ve completed the form, i got successfully sent.
Thanks
For support, please use the Visual Form Builder Pro Support Forums.
Sorry to be blunt, but what is the point in all this if you don’t mention where to put the code?
We aren’t all programmers, nor do we necessarily understand the inner workings of WordPress, nor your plugin.
Understandable. Code like this is typically placed in your theme’s
functions.phpfile.Hi Matthew,
Where do I put this code? What folder?
Is it under C:\inetpub\wwwroot\wp-content\plugins\visual-form-builder-pro?
Thank You.
This can go in your theme’s
functions.phpfile or a new plugin. Most people code like this infunctions.php.Hi Matthew,
Thanks for your reply. I have tried to enter the code under my theme’s function.php but it won’t work.
Can you give me a sample if I want to save my upload files to my C:\Users\Public\vfb-uploads or it should be inside wwwroot folder?
Thank You for helping.
I’m not familiar with a Windows server, but the WP_CONTENT_DIR and WP_CONTENT_URL constants can be changed to use whatever file path works for you.
Hi Matthew,
Is this code will work on Windows or are there any available codes for Windows based to customize the upload directory?
Thank You very much.
Hi Matthew,
I put this code to my functions.php file, but nothing happens. I run a multisite (path based) network wordpress site (version 3.5.1) and i have installed the latest Pro version of the script.
Multisite might require that you load things into the blogs.dir directory. This answer on StackExchange might help.
Hi Matthew, having a little problem with this code. I put it in the functions.php of the theme and its still uploading to uploads and not to the folder I created. Is there anything else I need to change besides the $dir and $url in order to get this code to work correctly?
You need to change the field name in the
$_FILESglobal.Hi Matthew, I’m not really understanding what to do here. Can you do me a favor and fill out the code if I give you a hypothetical situation?
URL: http://www.example.com
Upload folder name: vfb-uploads
folder location: http://www.example.com/vfb-uploads
Sorry for my lack of php knowledge. Thanks!
Help! I’m trying to get this to work for a particular form.
With what do I replace the string ‘vfb-file-upload-611′?
I have tried both using the form id and the field id in place of the 611 but neither works.
Hello Matthew!
Is there any way to bypass the media library and just upload to the server?
Cheers
Alex
Sorry if this was already answered anywhere else – but how can I handle the image size/max upload size? I get server errors with some image sizes. Is there a way the form can catch this errors and show them? Right now its just leading into a server error.
May be it would be a good option to open a modal window after submission showing the submission is in progress so people dont hit the button again as the image loads up…and ma ybe show the error in that modal when the image is to big.