Using Gravity Forms ‘gform_rest_api_capability_delete_entries’ PHP action

The gform_rest_api_capability_delete_entries filter allows you to modify the capability required to delete entries via the REST API v2 in Gravity Forms.

Usage

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

Parameters

  • $capability (string): The capability required. Defaults to ‘gravityforms_delete_entries’.
  • $request (WP_REST_Request): Full data about the request.

More information

See Gravity Forms Docs: gform_rest_api_capability_delete_entries

Note: This code should be placed in the functions.php file of your active theme.

Examples

Change the required capability

Change the required capability for deleting entries via the REST API v2 to ‘my_custom_capability’:

add_filter('gform_rest_api_capability_delete_entries', 'get_rest_api_capability_delete_entries', 10, 2);
function get_rest_api_capability_delete_entries($capability, $request) {
    return 'my_custom_capability';
}

Restrict deletion to specific form ID

Allow users with ‘editor’ role to delete entries only from form with ID 5:

add_filter('gform_rest_api_capability_delete_entries', 'restrict_deletion_to_specific_form', 10, 2);
function restrict_deletion_to_specific_form($capability, $request) {
    $form_id = $request->get_param('form_id');
    if ($form_id == 5) {
        return 'edit_others_posts';
    }
    return $capability;
}

Restrict deletion based on user role

Allow users with ‘editor’ role to delete entries, but prevent users with ‘author’ role from deleting entries:

add_filter('gform_rest_api_capability_delete_entries', 'allow_editors_to_delete_entries', 10, 2);
function allow_editors_to_delete_entries($capability, $request) {
    if (current_user_can('editor')) {
        return 'edit_others_posts';
    }
    if (current_user_can('author')) {
        return 'do_not_allow';
    }
    return $capability;
}

Restrict deletion based on entry creation date

Prevent deletion of entries older than 30 days:

add_filter('gform_rest_api_capability_delete_entries', 'prevent_old_entry_deletion', 10, 2);
function prevent_old_entry_deletion($capability, $request) {
    $entry_id = $request->get_param('entry_id');
    $entry = GFAPI::get_entry($entry_id);

    $entry_date = strtotime($entry['date_created']);
    $days_old = (time() - $entry_date) / (60 * 60 * 24);

    if ($days_old > 30) {
        return 'do_not_allow';
    }
    return $capability;
}

Restrict deletion based on entry value

Prevent deletion of entries with a specific value in field 3:

add_filter('gform_rest_api_capability_delete_entries', 'prevent_specific_value_deletion', 10, 2);
function prevent_specific_value_deletion($capability, $request) {
    $entry_id = $request->get_param('entry_id');
    $entry = GFAPI::get_entry($entry_id);

    if ($entry[3] == 'restricted_value') {
        return 'do_not_allow';
    }
    return $capability;
}