I am trying to prevent duplicate entries for 2 of my Gravity Forms. I need to prevent these entries only within a certain timeframe and based on certain fields like 'email' or 'address'. So if they have an entry with the same 'email' prior to the start_date, they can still submit the form without issue. If they have an entry with the same 'email' after the start_date and before the end_date then they won't be able to submit the form. The code below is what I currently have in place, but it isn't working. Any help is greatly appreciated.
add_filter( 'gform_is_duplicate', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) { $form_id = array( 6,61 ); $search_criteria = array( 'start_date' => 2021-02-01, // Get entries from date after last event 'end_date' => 2022-01-20, // Up to right before next event 'field_filters' => array( // which fields to search array( 'key' => 'email', ) ) ); $search_past_entries = GFAPI::count_entries($form_id, $search_criteria); if ( $search_past_entries >= '1' ) { echo 'You have already signed up.'; }
} 1 Answer
I managed to get this figured out with the help of a fellow developer...
add_filter( 'gform_is_duplicate_6', 'custom_duplicate_entry_rules', 10, 4 );
add_filter( 'gform_is_duplicate_61', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) { $search_criteria = array( 'start_date' => '2021-02-01', // Get entries from date after last event 'end_date' => '2022-01-20', // Up to right before next event 'field_filters' => array( // which fields to search array( 'key' => 2, 'value' => $value, ) ) ); $count = GFAPI::count_entries( $form_id, $search_criteria ); if( $count >= 1 ) { return $count; } else { return 0; }
}I needed to zero in on the specific entry meta in field_filters which in this case ended up being 2, for the email address (this is the field ID in Gravity Forms). Lastly, I changed the if statement to return $count or 0. Gravity Forms does all the heavy lifting beyond that.