The gform_rest_api_capability_get_results filter allows you to modify the capability required to get form results via the Gravity Forms REST API v2.
Usage
add_filter('gform_rest_api_capability_get_results', 'your_function_name', 10, 2);
Parameters
- $capability (string) – The capability required. Defaults to ‘gravityforms_view_entries’.
- $request (WP_REST_Request) – Full data about the request.
More information
See Gravity Forms Docs: gform_rest_api_capability_get_results
Examples
Change the required capability
Modify the required capability to access form results through the REST API.
add_filter('gform_rest_api_capability_get_results', 'get_rest_api_capability_get_results', 10, 2); function get_rest_api_capability_get_results($capability, $request) { return 'my_custom_capability'; }
Allow access based on user role
Allow only users with the ‘editor’ role to access form results through the REST API.
add_filter('gform_rest_api_capability_get_results', 'allow_editors_to_get_results', 10, 2); function allow_editors_to_get_results($capability, $request) { $user = wp_get_current_user(); if (in_array('editor', $user->roles)) { return $capability; } return 'do_not_allow'; }
Restrict access to specific form
Restrict access to form results through the REST API for a specific form.
add_filter('gform_rest_api_capability_get_results', 'restrict_specific_form_results', 10, 2); function restrict_specific_form_results($capability, $request) { $form_id = $request->get_param('form_id'); if ($form_id == 5) { return 'do_not_allow'; } return $capability; }
Allow access for specific IP addresses
Allow access to form results through the REST API only for specific IP addresses.
add_filter('gform_rest_api_capability_get_results', 'allow_specific_ips', 10, 2); function allow_specific_ips($capability, $request) { $allowed_ips = ['192.168.1.1', '192.168.1.2']; if (in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) { return $capability; } return 'do_not_allow'; }
Conditionally allow access based on form metadata
Allow access to form results through the REST API only if the form has a specific meta key.
add_filter('gform_rest_api_capability_get_results', 'allow_based_on_form_meta', 10, 2); function allow_based_on_form_meta($capability, $request) { $form_id = $request->get_param('form_id'); $meta_key = 'custom_meta_key'; $meta_value = gform_get_meta($form_id, $meta_key); if ($meta_value == 'some_value') { return $capability; } return 'do_not_allow'; }