Using Gravity Forms ‘gform_upload_path’ PHP filter

The gform_upload_path Gravity Forms PHP filter allows you to change the location where files are stored when uploaded using the File Upload or Post Image fields.

Usage

A generic example of how to use the filter:

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 the upload folder.
    • $path_info[‘url’] (string): Full URL to the upload folder.
  • $form_id (integer): The ID of the form currently being processed.

More information

See Gravity Forms Docs: gform_upload_path

Examples

Log the current path

This example logs the current path when logging is enabled. This helps you find out the current path before changing it:

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 );

Change the path and url

This example changes the upload path and url for the File Upload field. Note that the default secure “gf-download” links will work only with the default upload path, so 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;
}

Change the path

This example changes the path of the file to be deleted.

add_filter('gform_file_path_pre_delete_file', 'change_file_path', 10, 2);

function change_file_path($file_path, $url) {
    $file_path = '/home/public_html/yourdomainfolder/new/path/filename.pdf';

    return $file_path;
}

Change the file path based on form ID

This example changes the file path based on the form ID.

add_filter('gform_file_path_pre_delete_file', 'change_file_path_by_form_id', 10, 4);

function change_file_path_by_form_id($file_path, $url, $form_id, $field_id) {
    if ($form_id == 5) {
        $file_path = '/home/public_html/yourdomainfolder/new/path/form5/filename.pdf';
    }

    return $file_path;
}

Prevent file deletion for a specific form

This example prevents file deletion for a specific form by returning an empty path.

add_filter('gform_file_path_pre_delete_file', 'prevent_file_deletion', 10, 4);

function prevent_file_deletion($file_path, $url, $form_id, $field_id) {
    if ($form_id == 3) {
        return '';
    }

    return $file_path;
}

Delete files from a custom location

This example deletes files from a custom location using the gform_upload_path filter.

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/custom/uploads/';

    return $path_info;
}

add_filter('gform_file_path_pre_delete_file', 'delete_from_custom_location', 10, 4);

function delete_from_custom_location($file_path, $url, $form_id, $field_id) {
    $file_path = '/home/public_html/yourdomainfolder/custom/uploads/filename.pdf';

    return $file_path;
}

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.