Using Gravity Forms ‘gform_secure_file_download_location’ PHP filter

The gform_secure_file_download_location filter allows you to customize whether the real file location should be hidden or displayed for file uploads in Gravity Forms.

Usage

To apply the filter to all forms, use the following code snippet:

add_filter('gform_secure_file_download_location', 'your_function_name', 10, 3);

To target a specific form, append the form ID to the hook name:

add_filter('gform_secure_file_download_location_5', 'your_function_name', 10, 3);

Parameters

  • $secure_download_location (bool): Determines if the secure location should be used. Defaults to true.
  • $file (string): The URL of the file.
  • $field (GF_Field_Fileupload): The field associated with the file upload.

More information

See Gravity Forms Docs: gform_secure_file_download_location

Note: It is not recommended to display the real file location due to potential security risks. Use with caution.

Examples

Disable secure file download location for all forms

This code snippet disables the secure file download location for all forms, showing the real file location instead.

add_filter('gform_secure_file_download_location', '__return_false');

Disable secure file download location for a specific form

This code snippet disables the secure file download location for form ID 5, showing the real file location instead.

add_filter('gform_secure_file_download_location_5', '__return_false');

Enable secure file download location based on user role

This code snippet enables the secure file download location for all users except administrators.

function check_user_role($secure_download_location, $file, $field) {
    if (current_user_can('administrator')) {
        return false;
    }
    return $secure_download_location;
}
add_filter('gform_secure_file_download_location', 'check_user_role', 10, 3);

Enable secure file download location for specific file types

This code snippet enables the secure file download location only for PDF files.

function secure_pdf_downloads($secure_download_location, $file, $field) {
    if (pathinfo($file, PATHINFO_EXTENSION) == 'pdf') {
        return true;
    }
    return $secure_download_location;
}
add_filter('gform_secure_file_download_location', 'secure_pdf_downloads', 10, 3);

Disable secure file download location for specific field

This code snippet disables the secure file download location for a specific field with the field ID 3.

function disable_secure_for_field($secure_download_location, $file, $field) {
    if ($field->id == 3) {
        return false;
    }
    return $secure_download_location;
}
add_filter('gform_secure_file_download_location', 'disable_secure_for_field', 10, 3);