Using Dynamic Population

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 and example of how to use each method of dynamic population to dynamically populate a field on your form!

Contents

Getting Started

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 an text field labeled Parameter Name. The value you enter here will act as a reference to this field so Gravity Forms knows which field to populate.

Allow-dynamic-population.png

Once you've entered your parameter name, you're ready to decide which method best fits your dynamic population needs.

Query Strings

Shortcode

Hooks

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

add_filter('gform_field_value_author_email', 'populate_post_author_email');
function populate_post_author_email($value){
    global $post;

    $author_email = get_the_author_meta('email', $post->post_author);

    return $author_email;
}

The first thing you would do with this code is copy and paste it into your theme's functions.php file. Next, let's break it down line by line to get a better understanding of what is happening.

add_filter('gform_field_value_author_email', 'populate_post_author_email');

With this line, we are registering a custom function populate_post_author_email to the gform_field_value_$parameter_name filter. All that means is when Gravity Forms is rendering the form on a page it will call our custom populate_post_author_email function and then populate the value our custom function returns into the field.

So how does Gravity Forms now which field the value should be populated for? The dynamic population parameter! Keep in mind, Gravity Forms will populate every field that has a population parameter.

We could also customize the filter to run on a different parameter name. Let's say our population parameter name is actually "hello_world". Here is how the code would look:

add_filter('gform_field_value_hello_world', 'populate_post_author_email');

Ready for the next bit of code?

Getting and Returning the Value

The next step is getting the value we want to populate into the field and then returning it so Gravity Forms can populate our field.

    global $post;

    $author_email = get_the_author_meta('email', $post->post_author);

    return $author_email;

In this example, we want to get the author's email based on the post that the Gravity Form is embedded in. To get access to all the juicy post details, we need to declare the $post object global. We can now access various properties of the current post.

One of those properties is the post_author which is the ID of the current post's author. Why do we need this? To get the author's email of course! We'll pass the author's ID and the author meta (aka 'email') we are looking for to the WordPress get_the_author_meta function. This function will then return the... you guessed it; author email.

Now all we have to do is return the author's email back to Gravity Forms which will automatically populate our field with the returned value.

Search the Documentation