Dynamically Populating a Field

Introduction

Using dynamic population in Gravity Forms allows you to dynamically populate a field with a value (or values depending on the field type). This value can be passed via Query Strings, Shortcode and/or Hooks. This walk-through will give you an example of how to use each method of dynamic population to dynamically populate a field on your form!

Getting Started

IMPORTANT: The Dynamic Population feature uses PHP to get the value(s) and populate the form field(s). Therefore it can’t be used in cached pages. This is not a Gravity Forms limitation but a consequence of using caching, which prevents PHP code from running.

There are three steps to configuring dynamic population:

  1. Specify which field should be populated
  2. Configure how the field should be populated
  3. Determine what value the field should be populated with

To get started, let’s talk about specifying which field should be populated. To specify a field for dynamic population, just click the Allow Field To Be Populated Dynamically checkbox on the field’s advanced tab. Doing so will display a text field labeled Parameter Name. The value entered here will act as a reference to this field so Gravity Forms knows which field to populate.

Please note: Avoid using a parameter name from this list of reserved WordPress terms.

Once you’ve entered your parameter name, you’re ready to decide how you want to populate the field.

Query String

You can populate a field via the query string by appending the dynamic population parameter you specified for the field to the end of your form URL along with your custom value.

http://siteurl.com/form-url/?your_parameter=value

Assuming that a Gravity Form is on the page at this URL, any field with the dynamic population parameter name your_parameter would be populated with the value value.

When choosing the parameter name for your fields, be sure to avoid using any of the WordPress reserved terms. Best practice is to use your own prefix to make your parameter names unique. For example, use sa_number instead of number.

In order to pre-select checkbox fields and multiselect fields, provide a comma-separated list of values. Values to be pre-selected in choice based fields are case sensitive.

Some hosts using aggressive caching techniques, like WP Engine, are known to cache not only pages but also query string parameters. So if you’re having trouble getting your fields dynamically populated, please double check your page and query string parameters are not cached.

When is this method useful?

Let’s say you have a list of real estate agents you’re displaying on your website. Each agent has a link to a contact form so users can contact each agent directly. Instead of creating a separate contact form for each agent, you can add the agent’s email as a parameter to the contact form link and set up an email field to be dynamically populated by that parameter name.

Don’t want the dynamically populated field to be visible to the user so they can’t modify the populated value? Easy. Just check Administrative under the Visibility option on the field’s advanced tab.

Shortcode

You can populate a field via the Gravity Forms shortcode by adding the field_values parameter. The field_values parameter accepts multiple dynamic population parameters separated by an ampersand (&).

[gravityform id="1" field_values="parameter_name1=value1&parameter_name2=value2"]

In this example, any fields with the dynamic population parameter parameter_name1 would be populated with value1. Fields with the dynamic population parameter parameter2 would be populated by value2.

When is this method useful?

Dynamically populating by shortcode can be useful when you are creating different posts that use the same form but require a custom value depending on which post the form is displayed on.

Imagine you have a few events you’d like to register attendees for. You’ve set up a post for each event and want to use the same registration form for each, changing only the event name in the form based on the event post from which the user is registering.

To do this you would set the dynamic population parameter of the event field to something like event_name and then pass this parameter with the actual event name in the Gravity Forms shortcode like so:

[gravityforms id="1" field_values="event_name=My Awesome Event"]

Block

You can populate a field via the Gravity Forms block by using the Field Values text box available in the Advanced tab of a Gravity Forms block. The field values text box accepts multiple dynamic population parameters separated by an ampersand (&).

parameter_name1=value1&parameter_name2=value2

In this example, any fields with the dynamic population parameter parameter_name1 would be populated with value1. Fields with the dynamic population parameter parameter2 would be populated by value2. If the Preview setting is also enabled in the current block’s Advanced tab you will see the block update in real time with the dynamically populated values in the editor.

When is this method useful?

This method provides the same benefits described in the Shortcode method above but in a more modern user friendly manner.

Hooks

If you aren’t familiar with PHP and/or hooks, don’t be intimidated by this method for populating your fields. It’s really quite easy. Let’s start with this super basic sample code.

add_filter( 'gform_field_value_your_parameter', 'my_custom_population_function' );
function my_custom_population_function( $value ) {
    return 'boom!';
}

This snippet would populate any field with the parameter your_parameter with the result of the function my_custom_population_function. In this example, that function returns the string boom!.

When is this method useful?

When isn’t this method useful? This method allows for the most flexible dynamic population but also requires a bit more knowledge to use. If you’d like to better understand this method check out the documentation on the gform_field_value_parameter_name filter as well as a more Dynamically Populating The Post Author.

Placement

This code can be placed in the functions.php file of the active theme, a custom functions plugin, or custom add-on. See also: Where Do I Put This Code?

Known Limitations

  • Ampersands cannot be used as a field name/parameter or value.
  • The Dynamic Population feature uses PHP to get the value(s) and populate the form field(s). Therefore it can’t be used in cached pages. This is not a Gravity Forms limitation but a consequence of using caching, which prevents PHP code from running.
  • Avoid using these reserved WordPress terms.