Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.