Dynamically Populate Gravity Forms Select Fields with WordPress Child Pages.

15 March 2024

In this blog post, we will explore how to dynamically populate Gravity Forms select fields with child pages from a specific WordPress page. This can be particularly useful when you want to provide a dropdown of related pages for the user to select from.

Configure the Select Field in Gravity Forms

Before we dive into the code, let’s first set up the select field in the WordPress admin dashboard.

  1. Navigate to the form you want to edit in Gravity Forms.
  2. Click on the form field where you want to add the select field.
  3. In the field settings on the right, click on the “Advanced” tab.
  4. Check the “Allow field to be populated dynamically” checkbox.
  5. In the “Parameter Name” field that appears, enter “service”.

This will allow the select field to be populated dynamically with the code we’ll add in the next section.

The Code

Here’s the PHP code that accomplishes this (place within your theme’s functions.php file):
// Populate Gravity Forms Select Field with Services Child Pages
add_filter( 'gform_pre_render', 'populate_select' );
add_filter( 'gform_pre_validation', 'populate_select' );
function populate_select( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || $field->inputName !== 'service' ) {
continue;
}
// Get the ID of the parent page
$parent_page = get_page_by_path( 'services' );
// Get the child pages
$child_pages = get_pages( array( 'child_of' => $parent_page->ID ) );
// Clear the choices array
$field->choices = array();
// Populate the dropdown with child pages
foreach ( $child_pages as $page ) {
$field->choices[] = array( 'text' => $page->post_title, 'value' => $page->post_title );
}
}
return $form;
}
// Populate Gravity Forms Select Field with Current Service Name
add_filter( 'gform_field_value_service', 'set_service_value' );
function set_service_value( $value ) {
if ( is_page() && $post = get_post() ) {
$parent_page = get_page_by_path( 'services' );
if ( $post->post_parent == $parent_page->ID ) {
return $post->post_title;
}
}
return $value;
}

How It Works

The first function, populate_select, is hooked into the gform_pre_render and gform_pre_validation filters. This function runs before the form is displayed and before it is validated, respectively. It loops through all the fields in the form, and if it finds a select field with the input name ‘service’, it populates that field with the child pages of the ‘services’ page.

The second function, set_service_value, is hooked into the gform_field_value_service filter. This function runs when the form is being populated with dynamic values. If the current page is a child of the ‘services’ page, it sets the value of the ‘service’ field to the title of the current page.

Other Uses

This approach can be adapted for other uses as well. For example, you could populate a select field with posts from a specific category, or with users with a specific role. You just need to replace the code that gets the child pages with code that gets the posts or users, and adjust the field and value accordingly.

Here are a couple of examples:

Populating a Select Field with Posts from a Specific Category
add_filter( 'gform_pre_render', 'populate_select' );
add_filter( 'gform_pre_validation', 'populate_select' );
function populate_select( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || $field->inputName !== 'post' ) {
continue;
}
// Get posts from a specific category
$posts = get_posts( array( 'category_name' => 'your-category-slug' ) );
// Clear the choices array
$field->choices = array();
// Populate the dropdown with posts
foreach ( $posts as $post ) {
$field->choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
}
return $form;
}

Populating a Select Field with Users with a Specific Role
add_filter( 'gform_pre_render', 'populate_select' );
add_filter( 'gform_pre_validation', 'populate_select' );
function populate_select( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || $field->inputName !== 'user' ) {
continue;
}
// Get users with a specific role
$users = get_users( array( 'role' => 'your-role-slug' ) );
// Clear the choices array
$field->choices = array();
// Populate the dropdown with users
foreach ( $users as $user ) {
$field->choices[] = array( 'text' => $user->display_name, 'value' => $user->ID );
}
}
return $form;
}

Remember, the power of Gravity Forms lies in its flexibility. With a bit of PHP, you can customize your forms to fit your exact needs. Happy coding!

play showreel