The gform_is_duplicate Gravity Forms PHP filter allows you to specify custom duplicate validation logic for fields marked with the “No Duplicates” rule.
Usage
A generic example of how to use the filter:
add_filter( 'gform_is_duplicate', 'your_function_name', 10, 4 );
To target a specific form, append the form id to the hook name:
add_filter( 'gform_is_duplicate_5', 'your_function_name', 10, 4 );
Parameters
- $count (integer): The number of duplicate entries. Filtering this value to 0 means there are no duplicates.
- $form_id (integer): The current form’s id.
- $field (Field Object): The current field being validated.
- $value (string): The value of the current field.
More information
See Gravity Forms Docs: gform_is_duplicate
Examples
Check if email already registered
This example checks if the email address is already in use. If so, consider the email a duplicate and return a count of 1; otherwise, return a count of 0.
add_filter( 'gform_is_duplicate', 'noDuplicateEMails', 10, 4 );
function noDuplicateEMails( $count, $form_id, $field, $value ) {
if ( $field->type == ’email’ && get_user_by( ’email’, $value ) ) {
return 1;
} else {
return 0;
}
}
Use GFAPI::count_entries
This example uses the GFAPI::count_entries() method, for form ID 1, to get a count of entries which have a payment_status of Paid and contain the submitted value for field ID 2.
add_filter( 'gform_is_duplicate_1', function( $count, $form_id, $field, $value ) { if ( $field->id == 2 && $count ) { $search_criteria = array( 'status' => 'active', 'field_filters' => array( array( 'key' => 'payment_status', 'value' => 'Paid', ), array( 'key' => '2', 'value' => $value, ), ), ); $count = GFAPI::count_entries( $form_id, $search_criteria ); } return $count; }, 10, 4 );