gform_upload_path

Description

This filter is executed before uploading a file (by the File Upload or Post Image fields). It can be used to change the location where uploaded files are stored.

Usage

The following would apply to all forms.

add_filter( 'gform_upload_path', 'your_function_name', 10, 2 );

To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_upload_path_FORMID)

add_filter( 'gform_upload_path_5', 'your_function_name', 10, 2 );

Parameters

  • $path_info array
    The upload path to be filtered. It is an associative array with two keys which must be specified.

    • $path_info[‘path’] string
      Full physical path to upload folder.
    • $path_info[‘url’] string
      Full URL to upload folder.
  • $form_id integer
    The ID of the form currently being processed.

Examples

1. Log the current path

You can use this example when logging is enabled to find out what the current path is before using the next example to change the path.

add_filter( 'gform_upload_path', function ( $path_info, $form_id ) {
    GFCommon::log_debug( "log_upload_path(): path_info for form #{$form_id} => " . print_r( $path_info, true ) );

    return $path_info;
}, 10, 2 );

2. Change the path and url

This example changes the upload path and url for the File Upload field.

Note: The default secure “gf-download” links will work only with the default upload path, therefore if you use this filter to modify that, you will not be able to use the “gf-download” links.

add_filter( 'gform_upload_path', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id ) {
   $path_info['path'] = '/home/public_html/yourdomainfolder/new/path/';
   $path_info['url'] = 'http://yourdomainhere.com/new/path/';
   return $path_info;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormsModel::get_file_upload_path() in forms_model.php.