Using Gravity Forms ‘gform_secure_file_download_url’ PHP filter

The gform_secure_file_download_url Gravity Forms filter allows you to manually filter the download URL for conditions such as unusual domain mapping and others.

Usage

To use this filter, add the following code to your active theme’s functions.php file:

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

Parameters

  • $download_url (string): The URL from which to download the file.
  • $field (GF_Field_Fileupload): The field object for further context.

More information

See Gravity Forms Docs: gform_secure_file_download_url

This filter was added in Gravity Forms 2.1.1.1.

Examples

Custom Domain Mapping

This example changes the download URL’s domain to a custom domain.

function custom_download_url($download_url, $field) {
    // Replace 'your-custom-domain.com' with your custom domain
    $new_domain = 'https://your-custom-domain.com';
    $download_url = str_replace(home_url(), $new_domain, $download_url);
    return $download_url;
}
add_filter('gform_secure_file_download_url', 'custom_download_url', 10, 2);

Add Extra Query Parameters

This example adds extra query parameters to the download URL.

function add_extra_query_params($download_url, $field) {
    // Add custom query parameters to the download URL
    $download_url = add_query_arg(array('utm_source' => 'example', 'utm_medium' => 'email'), $download_url);
    return $download_url;
}
add_filter('gform_secure_file_download_url', 'add_extra_query_params', 10, 2);

Change Download URL Path

This example changes the path of the download URL.

function change_download_url_path($download_url, $field) {
    // Replace 'your-new-path' with the desired new path
    $new_path = '/your-new-path';
    $download_url = preg_replace('#(/[^/]+)/*$#', $new_path, $download_url);
    return $download_url;
}
add_filter('gform_secure_file_download_url', 'change_download_url_path', 10, 2);

Restrict Download URL by User Role

This example restricts download URL access based on the user’s role.

function restrict_download_url_by_role($download_url, $field) {
    // Replace 'editor' with the desired user role
    $allowed_role = 'editor';
    if (current_user_can($allowed_role)) {
        return $download_url;
    } else {
        return '#';
    }
}
add_filter('gform_secure_file_download_url', 'restrict_download_url_by_role', 10, 2);

Change Download URL Protocol

This example changes the download URL protocol to HTTPS.

function force_https_download_url($download_url, $field) {
    // Force the download URL to use HTTPS
    $download_url = set_url_scheme($download_url, 'https');
    return $download_url;
}
add_filter('gform_secure_file_download_url', 'force_https_download_url', 10, 2);