The gform_permission_granted_pre_download filter in Gravity Forms allows you to implement custom logic to decide if a user is allowed to access a file through a generated download URL.
Usage
To use this filter, you can add the following code to your functions.php
file or a custom functions plugin:
add_filter('gform_permission_granted_pre_download', 'your_function_name', 10, 3);
Parameters
- $permission_granted (bool) – Indicates if the user has permission to access the file. Default is the result of the hash validation.
- $form_id (int) – The ID of the form used to upload the requested file.
- $field_id (int) – The ID of the field used to upload the requested file.
More information
See Gravity Forms Docs: gform_permission_granted_pre_download
Examples
Restrict access to users with administrative capabilities
This example restricts access to users with permission to activate plugins, but only when the hash has passed validation:
add_filter('gform_permission_granted_pre_download', function($permission_granted, $form_id, $field_id) { return $permission_granted && current_user_can('activate_plugins'); }, 10, 3);
Allow access to specific form only
This example allows file download access only if the form ID matches the specified form:
add_filter('gform_permission_granted_pre_download', function($permission_granted, $form_id, $field_id) { $allowed_form_id = 5; return $permission_granted && ($form_id == $allowed_form_id); }, 10, 3);
Allow access to files in specific field only
This example allows file download access only if the field ID matches the specified field:
add_filter('gform_permission_granted_pre_download', function($permission_granted, $form_id, $field_id) { $allowed_field_id = 8; return $permission_granted && ($field_id == $allowed_field_id); }, 10, 3);
Allow access only to logged-in users
This example allows file download access only to logged-in users:
add_filter('gform_permission_granted_pre_download', function($permission_granted, $form_id, $field_id) { return $permission_granted && is_user_logged_in(); }, 10, 3);
Allow access based on custom user meta value
This example allows file download access only if the user has a specific custom user meta value:
add_filter('gform_permission_granted_pre_download', function($permission_granted, $form_id, $field_id) { $user_id = get_current_user_id(); $custom_meta_value = get_user_meta($user_id, 'custom_meta_key', true); return $permission_granted && ($custom_meta_value == 'allowed'); }, 10, 3);