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['vfb-submit']
    if ( isset( $_FILES['vfb-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-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.


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.