There are two ways you might want to populate a drop down field. The first way is pre-selecting a drop down option when the form is displayed. The second way is dynamically populating the options that are available in a select (aka drop down) field. This walk-through will teach you how to configure the latter.
The premise for this walk-through is that we want to populate the options in a Gravity Form select field with all of the posts currently published on the site. There are a variety of reasons for wanting to do this, but in this example, we just want to know what the user's favorite post is.
Contents |
The Code
This code should be pasted in your theme's functions.php file.
Using the gform_pre_render Filter
Using the gform_pre_render filter allows us to modify the form right before it is displayed. This is really useful, especially when you are looking to display content that will be constantly changing.
In our case, this saves us the time and hassle of having to update our drop down of posts every time we add a new post.
add_filter('gform_pre_render_51', 'populate_posts');
This line tells Gravity Forms to filter the $form object through the populate_posts function every time the form with ID of 51 is displayed. You should update the 51 here to the ID of your form.
Next, let's create a populate_posts function to modify our form.
Custom Filter Function
In this example, our custom filter function is titled "populate_posts". Since we told Gravity Forms to call this function from the gform_pre_render filter, Gravity Forms will also send the $form object as a parameter to this function. This means we can modify the $form object with this function and then return our changes at the end of the function.
Looping Through the Fields
foreach($form['fields'] as &$field){ ... }
Since we have the $form object, let's loop through the fields in the form and find the field(s) that we'd like to modify.
if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
continue;
Given our premise, we only want to modify fields that are select (drop down) fields AND have a CSS class titled "populate-posts". We could check for the field ID instead of a CSS class; however, if we ever change our form we'd have to update the code to match the new field ID. Using a CSS class to identify our desired field allows us to manage which field should be targeted right from the Gravity Forms admin.
If the current field we are at in the loop is not a select OR does not have our designated CSS class, we simply skip that field and move on to the next.
Get the Posts
Now it's time to get an array of all the posts to display in our drop down.
$posts = get_posts('numberposts=-1');
get_posts() is a WordPress function that returns an array of posts based on the parameters you provide it. For more information on how to modify which posts are returned, read here.
Quick Tip You can retrieve any kind of post type here; even custom post types. Just pass the name of your post type with the "post_type" parameter:
$posts = get_posts('post_type=event&numberposts=-1');
Creating an Array of Choices
The way Gravity Forms handles fields with multiple options (such as a select field) is with a "choices" property for the field which contains an array of "options". Each option consists of:
- a "text" property (which is what the user will see in the drop down)
- a "value" property (which is what will be recorded by Gravity Forms)
- an "isSelected" property (which is used to indicate whether the option is currently selected or not)
- and a "price" property (which only applies to pricing fields).
In our scenario, we are only concerned with the "text" and "value" properties.
$choices = array(array('text' => 'Select a Post', 'value' => ' '));
The first thing we do is create an array to store all of our options AND we'll go ahead and create our first option array inside the choices array as well. This first option is what we'll call the "instructive" option. It let's the user know what they are supposed to do with this drop down (ie "Select a Post").
You'll also notice that we're passing an empty value for the "value" property. This ensures that if this is a required field, that the user must select an option other than the instructive option. Failing to do so will generate a validation error.
foreach($posts as $post){
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
}
Next, we're going to loop through the posts we retrieved using the get_posts() function and create an option array for each post. For the "text" property we will use the Post Title and for the "value" property we will use the same. In some cases you may want to collect the ID of the selected post. If so you'd use $post->ID instead of $post->post_title; however, for our goal of recording the users favorite post, the post title is sufficient.
$field['choices'] = $choices;
Once we've looped through all of the posts and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
Conclusion
That's all there is to it! When you view your form on the front end you should now see that your select field is populated with a list of all your current posts.