Web Analytics

Gravity Forms: Add Registration Limits for Complex Forms

by Utsab Karki, Senior WordPress Developer

Photo by Prime Cinematics via Unsplash

Forms are an integral part of a website, whether it’s a simple contact form for lead generation or something more complex, like event registrations.

While there are a number of form options that that we recommend, the one that stands out the most for WordPress is Gravity Forms.

It is a versatile plugin loaded with features and a library of options out of the box that can be easily used to create any kind of form. It also provides a number of add-ons that can be used to further extend it’s functionality based on the website’s needs.

When using Gravity Forms for event registrations, one of the common requests we get is for the ability to close the form when a specific number of people have registered. If you are using the form to register a fixed number of individuals per form submission (ie, 1 person per entry), then out of the box, Gravity Forms has a way to limit the number of entries.

But if different numbers of people can be registered with each form submission, then we need a custom solution to add the total number of registrants and only deactivate the form if the limit has been reached.

Here’s how.

Tracking and Counting the Number of Registrants

In order to limit the form to a specific number of registrants, we will need a dedicated field that keeps track of the total number of registrants for each form submission.

This tracking field can be built in more than one way.

If your form uses a number field that allows the user to simply input the number of individuals that they’d like to register, then we can utilize a second built-in number field to track the total. But if you have a complex form with more than one field indicating the number of registrants (such as separate fields for number of adults and number of children), then you need to add a hidden field to the form and utilize the gform_pre_submission action hook to update the field with the total number of registrants for the submission.

For the following example, we assume that the form ID is 1 and it has two fields: one for total number of adults and one for total number of children. Let’s assume that the fields IDs for these fields are 1 and 2 and the field ID for the hidden field is 3.

add_action( 'gform_pre_submission_1', function ( $form ) {
    $total_registrants = rgpost('input_1') + rgpost('input_2');
    $_POST['input_3'] = $total_registrants;
});

In the above code, we add the total number of adults and total number of children and assign the value to the hidden field when the form is submitted.

Now that we have a field indicating how many are registered in each submission, we can use the gform_pre_render filter to either display the form or hide it (and display a message) based on the registration limit.

For the registration limit, we are going to utilize the built-in form restrictions to enable the registration limit, set a registration limit, and add a message to  display instead of the form when the limit is reached.

In the below code, we use the gform_pre_render_1 filter to target the form with ID 1. We initialize the $num_registrants variable with a value of 0 to keep track of the total number of registrants across all submissions, and use the Gravity Forms API’s get_entries function to get all the active submissions for the form and assign it to the $form_submissions variable.

Then we loop through each submission and add the total number of registrants to the $num_registrants variable.

After looping through all the submissions, we check to see if “Enable entry limit” is checked in the form’s Settings. If it is checked, the $num_registrants value is checked against the number entered in the admin, and if the limit has been reached, we update the value in the admin to 1. This forces Gravity Forms to hide the form and display the message added in the admin if the registration limit has been reached.

add_filter( 'gform_pre_render_1', function ( $form ){
    $num_registrants = 0;
    // Get Active submissions
    $form_submissions = GFAPI::get_entries( $form['id'], array('status' => 'active'), null, null ); 
    foreach ($form_submissions as $single_submission) {
        // Add Total Registrants field from each submission
        $num_registrants += intval(rgar( $single_submission, '3' )); 
    }
    // If form has Entry Limit Enabled
    if ($form['limitEntries']) { 
        if ($num_registrants >= $form['limitEntriesCount']) { 
            $form['limitEntriesCount'] = 1;
        }
    }
    return $form;
});

By utilizing the hooks and filters provided by Gravity Forms, we can streamline the process of registering for an event without having to worry about overbooking.

If you need help implementing registration limits for your Gravity Forms, reach out to us and we’d be more than happy to assist you in your web development and digital marketing needs.

Avatar photo
About Utsab Karki

Utsab Karki is the Senior WordPress Developer at Digital Ink. He builds new websites, manages functionality requests and changes, and makes clients' sites run better. Digital Ink tells stories for forward-thinking businesses, mission-driven organizations, and marketing and technology agencies in need of a creative and digital partner.

Other Stories You May Like

What’s your story?

Let’s share it with the world.

Let’s Do This

Close Window
All the Cool Kids are Doing it

Sign Up for the Digital Ink Newsletter

All the cool kids are doing it.