Contents
Introduction
Gravity Forms is one of the most powerful WordPress form plugins, allowing businesses and website owners to create dynamic and user-friendly forms. While many fields are commonly included, one often overlooked but highly valuable addition is the Country field. Here’s why incorporating a country field into your Gravity Forms is not just useful but essential for user experience and data management. In this article, I’ll teach you how to add a country field to your form(s) and use the geolocation API from the https://ipinfo.io/ service to automatically populate that field with the relevant country code. Here are a couple of reasons why could that be handy:
1. Enhancing User Experience
Adding a country field makes it easier for users to provide their location without typing unnecessary details. Instead of manually entering their country name, each entry’s IP field will be used to geolocate the users automatically.
2. Standardized Data Collection
Manually typed country names can lead to inconsistencies due to spelling variations, abbreviations, or typos. An automatic, IP based location field prevents these errors from happening.
3. Improved Personalization & Localization
By collecting country information, businesses can personalize their services based on user location. Whether it’s displaying local pricing, suggesting relevant services, or sending geo-targeted emails, having country data enables better customer engagement. For businesses that rely on form submissions for lead generation or customer interactions, knowing where users are coming from allows for more detailed analytics. This data can be invaluable for marketing strategies and business expansion planning.
Gravity Forms Setup
- Go to Forms -> New Form -> Blank Form add a title and press Create Blank Form.
- Add a number of fields, say name, email etc.
- Add a new, hidden field.
- Remember the id of the field you have just created.
- Gravity Forms hidden field
- Gravity Forms hidden field ID
Custom Code
In the next step of our tutorial, we’ll use the Gravity Forms after_submission hook. This part of code will be executed after our form was submitted, and an entry was created. We’ll take advantage of that, we’ll use the submissions IP address (which is collected by Gravity Forms by default) and we’ll use that IP to fetch the country code next. The case is pretty simple. If you have a Child Theme, place that code in the functions.php of your child theme. If not, either create one, or create a plugin or mu-plugin yourself. Note: you’ll need to register on the ipinfo.io service, to obtain the token to be used with the code.
Outcome
Assuming everything went right and you did setup the code properly, each Gravity Forms entry will have a its Country Field populated with the Country Code. This can be later on exported, filtered out or used according to your needs. To access the Gravity Forms entries go to:
- Forms -> Forms
- Hover over the form and click on entries.
- Click on the gear icon and drag and drop User IP and your field from right to left.
- If everything went right, you should see something similar to the below image.
Conclusion
Gravity Forms,