Auto-Populate Gravity Forms Select Dropdown with values from Advanced Custom Fields (ACF)

In a recent project I was tasked with populating a custom post type with user submitted forms for active members. This handy plugin takes care of allowing CPT creation directly via Gravity Form submissions, so that part was simple.

One of the fields in the form was a department dropdown. While creating the select field is simple enough, it was vital that the field stay in sync with an Advanced Custom Fields (ACF) select field that would have values added from time to time.

The “simplest” way to do this is to make sure every time you edit the ACF field you also edit the form, but this would be an easy step to forget, especially if more than one user is going to be editing the form. To prevent this issue and simplify things, I worked out a way to auto-populate the Gravity Forms dropdown directly from the ACF Field. The code below would be placed in your functions.php or equivalent.

/*
 * Autopopulate Dropdown with ACF Select Fields (Form ID 1)
 *
 * @uses https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
 *
 */
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
     foreach ( $form['fields'] as &$field ) {

          // Only populate field ID 12
          if( $field['id'] == 12 ) {

               $acf_field_key = "field_5c2e737c0c58e"; // ACF field key
               $acf_field = get_field_object($acf_field_key); // Object Field of selected field
               $choices = array(); // Set up blank array

               if( $acf_field ) {
                    // loop over each select item at add value/option to $choices array
                    foreach( $acf_field['choices'] as $k => $v ) {
                         $choices[] = array( 'text' => $v, 'value' => $k );
                    }
               }

               // Set placeholder text for dropdown
               $field->placeholder = '-- Choose a Department --';

               // Set choices from array of ACF values
               $field->choices = $choices;

          }
     }
return $form;
}

Simply put, each of the filters are called just before the form renders, so this function will run just before the form is created, on both the front end and the admin dashboard (due to the gform_admin_pre_render filter).

How does this work?

  1. Check to ensure we’re targeting the correct form by adding _1 to the end of each filter
  2. Check to ensure we are targeting the correct field on the form with the $field['id'] check
  3. Grab the correct ACF field using the ACF field key
  4. Get the object field from that ACF key
  5. Loop over each choice in the ACF object and set it as an array
  6. Populate the Gravity Forms field with the array created using the ACF Field

A Few Tips

  • Note that it is recommended to use the field key to ensure it works properly if the name of the field is changed in the future. You will want to swap out the key in the example above with your own ACF field key
  • Make sure you change out the _1 after each filter with the ID of the form you are targeting
  • You will also want to change the $field['id'] to the ID of the field you are targeting within that form

Comments

  1. Lee Kancher says:

    Great article. I have a question. If I wanted to populate the ACF field with taxonomy(which I have working ) and then have the gravity form field populate with those choices. The ACF field is set as a multi select. Thanks for your time in advance.

    1. Matt Whiteley says:

      Hey Lee. If you are populating the ACF field with a taxonomy, you could use get_terms within the function in this article and loop over the taxonomy terms and set them as your options for the Gravity Forms field (see: https://developer.wordpress.org/reference/functions/get_terms/). Then as you update your taxonomy it would automatically update both your ACF field and your Gravity Forms field.

      1. Lee says:

        Hey Matt, Thanks. We are going with the code you have above and it seems to be working great. It populates the Form Field so user can select on front end but it does not send the data back to the acf field in my custom post. Im pretty sure my Custom Field Names are correct in the gravity form.

        On the gform it is a drop down and in acf the field is select box. Am I missing something? Thank you for all the support. Cheers.

        *We are using plugin: Gravity Forms + Custom Post Types

        1. Matt Whiteley says:

          Hey Lee – without looking at your code I can’t really be of too much help. Just based on your description it sounds like the settings on the Gravity Form that feed the CPT creation may notn be 100% correct. Feel free to email me ([email protected]) more detail or a screenshot of your setups and I could take a quick look.

          1. Lee says:

            Ok, Things looking good. I have solved most of the populating issues. Just one more quick one. I am trying to populate gravity form from acf field that is a select2.

  2. Chris says:

    Hi Matt, I can’t thank you enough for this tutorial. I’ve been working on a form where I got stuck with ACF data. Your code worked perfectly. It also provided a use case for ACF’s get_field_object function. I’ve never had to use it before. My best to you and yours

    1. Matt Whiteley says:

      Glad I could help! Cheers!

  3. Marc W. says:

    Thanks for this code. Could you possibly explain how your ACF field is set up? I am trying to populate the dropdown with a field from a repeater.

Leave a Reply

Your email address will not be published. Required fields are marked *