how to populate fields dynamically

Gravity Forms: Populate Fields Dynamically (Ultimate Guide)

Written by Casey Burridge

Last updated:

Categories Gravity Forms

So you need a way to dynamically populate Gravity Forms field values? 

This functionality is useful if you have the same form on different pages and you need a way to differentiate between submissions. It’s also helpful for personalizing and streamlining the form-filling process.

In this post, we’ll look at the following four ways to pre-populate form fields in Gravity Forms:

  1. Using a shortcode
  2. Using URL query string parameters
  3. The Gutenberg block method
  4. Using a hook and custom code

Ready? Let’s get started! 😁

Why Pre-Populate Fields in Your Form?

Prepopulating field values in a form is helpful if you’re using the same form to collect information for different things and you need a way to differentiate between submissions. 

For example, let’s say you’re using the same sign-up form on multiple landing pages and you need a way to check which page the user came from. You could pre-fill a form field with the name of the page and include this in the user’s submission.

Enabling Dynamic Field Population

Although there are several different methods for dynamically populating fields in Gravity forms, the first step is always the same, and that’s to enable dynamic field population on the field itself!

To do this, open your form editor and click on the field you want to populate dynamically. Next, open the Advanced tab under Field Settings on the right. Now scroll down and check the box that says “Allow field to be populated dynamically”.

Finally, give your field a parameter name, this is how you’ll target the field in order to change its value. The parameter name can be anything (it’s for your reference only).

The Parameter Name field contaning 'text_field'

Now that you’ve enabled dynamic population, you can use one of the methods below to pre-populate your form field.

Dynamically Populate Fields Using a Shortcode

The Gravity Forms shortcode allows you to display forms anywhere on your website. The shortcode also accepts a number of parameters allowing you to enable ajax, show the title/description, and populate fields dynamically.

In this case, the parameter we’re interested in is the field_values parameter. Here’s an example of what the shortcode looks like containing this parameter.

[gravityforms id=“21” field_values=“text_field=Greetings, Earthlings!”]

Note: The id parameter is required and this corresponds to the ID of your form in Gravity Forms. You can find your form ID on the Forms page in the column to the right.

As you can see, the field_values parameter is followed by the parameter name we gave to our field and then the value we want to populate the field with. If you want to populate multiple fields, you can do this by using an ‘&’ to add multiple field values. Here’s how that would look.

[gravityforms id=“21” field_values=“text_field=Greetings, Earthlings!&another_field=Hey there”]
A form with two text fields, the first one says 'Greetings, Earthlings!', the second one says 'Hey there'

When to Use This method

The shortcode method is useful if you want to display the same form on different pages but you require a custom value based on which page the form is on. 

URL Query String Parameters

Another way to populate fields dynamically in Gravity Forms is to use URL parameters. To do this, append the parameter name of your field along with the value as a key/value pair to the end of the URL, like this:

https://example.com/event-registration/?event_name=Charity%20fun%20run

In this example, the field with the event_name parameter will be populated with “Charity fun run”. Note that we’re using ‘%20’ to denote a space as internet browsers don’t accept spaces in URLs. In fact, all special characters (such as exclamation marks) need proper encoding. If you’re not sure how to do this, check out this free online tool.

When to Use This Method

Let’s say you own a retail business with multiple store locations and each location has a link to a contact form. Instead of creating a separate form for each store, you could use query parameters to pre-populate a field on a single contact form with the store name so you know which store the user wants to contact.

Gravity Forms Gutenberg Block Field Values

The Gravity Forms Gutenberg block also allows you to pre-populate field values. To start you’ll need to add the block to your page or post by opening the Gutenberg editor and searching for the Gravity Forms block.

Next, select your form from the dropdown menu. After that, you should see a preview of your form load inside the editor.

The Gravity Forms Gutenberg block with an arrow pointing to the 'Select a Form' dropdown.

To configure dynamic field population, open the block settings on the right and enter the field parameter followed by the value you want to populate it with inside the Field Values text box. Your form preview should now update inside the editor, showing the value inside the field.

An arrow pointing to the 'Field Values' box

When to Use This Method

If you prefer to display Gravity Forms using the Gutenberg block instead of the [gravityforms] shortcode, this method offers a simple way to pre-populate field values.

Using a Hook

If you’re comfortable adding custom PHP code to your WordPress theme, you can use a hook to dynamically populate form fields in Gravity Forms. For more information about this method, check out the Gravity Forms documentation regarding the gform_field_value_$parameter_name filter.

This method is the most powerful and most flexible out of the four, but it also requires some basic knowledge of WordPress code.

Pre-selecting Options in a Drop Down or Checkbox Field

So far we’ve shown you how to pre-populate text fields in Gravity Forms but what if you need to pre-select options in a drop down, checkbox, or radio field? Well, that’s also possible and the method is almost identical.

When you’ve added the checkbox/drop down/radio field to your form, open the General tab in the Field Settings and check the box that says “Show values”. Now, ensure that the value you’re passing to the Pre-selecting Options in a Drop Down or Checkbox Field field_values parameter is the Value of the option you want to pre-select.

Gravity Forms Drop Down field settings showing the Label and Value for each choice

For example, if you wanted to pre-select the first option, “Coffee”, you would pass the value “first choice”, like so:

[gravityforms id="21" field_values="dropdown=first choice"]

Bonus: Dynamically Populating Field Choices

So far we’ve covered how to dynamically populate field values in Gravity Forms, but what if you want to dynamically populate choices in a dropdown, radio button, or multi-select field?

You can do this using a third-party add-on called Populate Anything by Gravity Wiz. Populate Anything allows you to populate field choices with posts, users, taxonomies, terms, Gravity Forms entries, databases, and more.

After installing the plugin, open your form editor and add a drop down, radio button, or multi-select field to your form. Next, open the Field Settings and check the box that says “Populate choices dynamically”. 

A checkbox that says 'Populate choices dynamically'

Now choose what you want to populate the choices with by selecting an option from the Type drop down menu. For example, if you choose “Users”, your dropdown field will contain a list of WordPress users on your website.

Gravity Forms Dynamic Population: Final Thoughts

The ability to dynamically populate field values is a powerful feature of Gravity Forms. There are four different methods for dynamically populating a field in Gravity Forms – 1. the [gravityforms] shortcode; 2. URL query parameters; 3. custom code and; 4. the Gravity Forms Gutenberg block.

You can also dynamically populate field choices for drop downs, radio buttons, and multi-select fields using the Populate Anything “perk” by Gravity Wiz.

For more helpful tips and tricks, check out our comprehensive guide on how to use Gravity Forms.