25% off ProSNIPC25OFF

Gravity Forms: Limit Total Form Entries

Blocks new submissions once a Gravity Forms form reaches a configurable maximum entry count and shows the user a friendly closed-form message.

PHPby SnipCraft
php
<?php
if ( class_exists( 'GFForms' ) && ! function_exists( 'scseed_gf_limit_entries' ) ) {
    /**
     * Prevents submission when the total entry count for a form hits its limit.
     * Add form IDs and their caps to the $limits array below.
     *
     * Runs at priority 5 so the limit check fires before other validation rules.
     *
     * @param array $validation_result
     * @return array
     */
    function scseed_gf_limit_entries( $validation_result ) {
        // Map form ID (int) => max entries (int).
        $limits = [
            1 => 100,
            7 => 50,
        ];

        $form    = $validation_result['form'];
        $form_id = (int) $form['id'];

        if ( ! isset( $limits[ $form_id ] ) ) {
            return $validation_result;
        }

        $max         = (int) $limits[ $form_id ];
        $entry_count = GFAPI::count_entries( $form_id );

        if ( $entry_count >= $max ) {
            $validation_result['is_valid'] = false;
            foreach ( $form['fields'] as &$field ) {
                $field->failed_validation  = true;
                $field->validation_message = sprintf(
                    'This form is now closed. The maximum of %d submissions has been reached.',
                    $max
                );
                break;
            }
            $validation_result['form'] = $form;
        }

        return $validation_result;
    }
    add_filter( 'gform_validation', 'scseed_gf_limit_entries', 5 );
}
#entry-limit#form-management#forms#gravity-forms#submissions