Limit Gravity Forms Entries per IP Address
Stops a visitor from sending more than a few Gravity Forms entries per hour from the same IP address, with a polite message, to block flooding and repeat spam.
Opens WPIDE on
Code
$wpide_gf_max_entries = 3; // Entries allowed per IP...
$wpide_gf_window = '1 hour'; // ...within this period.
$wpide_gf_limit_forms = array(); // Form IDs to limit; empty = all.
add_filter( 'gform_validation', function ( $validation ) use ( $wpide_gf_max_entries, $wpide_gf_window, $wpide_gf_limit_forms ) {
$form = $validation['form'];
if ( ! $validation['is_valid'] || ( $wpide_gf_limit_forms && ! in_array( (int) $form['id'], array_map( 'intval', $wpide_gf_limit_forms ), true ) ) ) {
return $validation;
}
$count = GFAPI::count_entries( $wpide_gf_limit_forms ? (int) $form['id'] : 0, array(
'status' => 'active',
'start_date' => wp_date( 'Y-m-d H:i:s', strtotime( '-' . $wpide_gf_window ) ),
'field_filters' => array( array( 'key' => 'ip', 'value' => GFFormsModel::get_ip() ) ),
) );
if ( $count >= $wpide_gf_max_entries ) {
$validation['is_valid'] = false;
foreach ( $form['fields'] as $field ) {
if ( ! in_array( $field->type, array( 'hidden', 'html', 'section', 'page' ), true ) ) {
$field->failed_validation = true;
$field->validation_message = 'You have sent several messages recently. Please try again later.';
break;
}
}
$validation['form'] = $form;
}
return $validation;
} );
Notes
Counts entries already stored for that IP across all forms; set a form ID in the list to limit only specific forms.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.