Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristacheda/5db1798cc64825e732bf22aee1d511c4 to your computer and use it in GitHub Desktop.
Save cristacheda/5db1798cc64825e732bf22aee1d511c4 to your computer and use it in GitHub Desktop.
Populate Gravity Forms dropdown field with Advanced Custom Fields repeater values.

Add Advanced Custom Fields repeater values to a Gravity Forms dropdown field

The code should be added in a separate file and then included in your functions.php using require.

Replace 1 from the code with the ID of the form that you want to dinamically populate with field values from ACF. You can find more info about the filters in the Gravity Forms documentation.

add_filter( 'gform_pre_render_1', 'populate_dropdown' );
add_filter( 'gform_pre_validation_1', 'populate_dropdown' );
add_filter( 'gform_admin_pre_render_1', 'populate_dropdown' );
add_filter( 'gform_pre_submission_filter_1', 'populate_dropdown' );

if ( $form['id'] != 1 ) {
   return $form;
}

if ( $field->id == 1 ) {
    $field->choices = $dropdownitems;
}
<?php
if ( !is_admin() ) {
add_filter( 'gform_pre_render_1', 'populate_dropdown' );
add_filter( 'gform_pre_validation_1', 'populate_dropdown' );
add_filter( 'gform_admin_pre_render_1', 'populate_dropdown' );
add_filter( 'gform_pre_submission_filter_1', 'populate_dropdown' );
function populate_dropdown( $form ) {
if ( $form['id'] != 1 ) {
return $form;
}
$acf_repeater = get_field( 'repeater_field' );
// declare the array
$dropdownitems = array();
// add first & default value of the dropdown
$dropdownitems[] = array(
'text' => 'Default value of dropdown',
'value' => ''
);
foreach ($acf_repeater as $acf_repeater_subfield) {
$dropdownitems[] = array(
'value' => $acf_repeater_subfield['subfield_name'],
'text' => $acf_repeater_subfield['subfield_name']
);
}
//Add repeater values as dropdown options
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 1 ) {
$field->choices = $dropdownitems;
}
}
return $form;
wp_reset_postdata();
}
}
@zmayyem
Copy link

zmayyem commented Feb 11, 2021

Hello @cristacheda your code is perfect but it only work when subfield of acf repeater is text field only ,
But In my case I need to populate the gravity form with the a taxonomy name , can you help me please ??

So I have a acf repeater that contain a taxonomy subfield,
the default Return Value of ACF Taxonomy is "ID" or "Object".

If I select "Taxonomy Id" the population function work and I can see the Id of the selected taxonomy on the Gravity Form Dropdown.

But If I select "Taxonomy Object" The Gravity Form will disappear , and not work

So Please can you help to add the necessary codes to your snippet
to print Taxonomy name if the subfield of the repeater is Taxonomy field !!

Thanks in advance

@cristacheda
Copy link
Author

cristacheda commented Mar 2, 2021

Hello @zmayyem! I'm glad someone else found this bit of code useful.

To make it work with a Taxonomy Object field you need to tweak the foreach loop between lines 25 and 30. It should look like this:

Haven't tested the code but this should do it.

Good luck!

<?php

if ( !is_admin() ) {
  add_filter( 'gform_pre_render_1', 'populate_dropdown' );
  add_filter( 'gform_pre_validation_1', 'populate_dropdown' );
  add_filter( 'gform_admin_pre_render_1', 'populate_dropdown' );
  add_filter( 'gform_pre_submission_filter_1', 'populate_dropdown' );

  function populate_dropdown( $form ) {
    if ( $form['id'] != 1 ) {
       return $form;
    }

    // Output set to: Object
    $terms = get_field( 'taxonomy_field_name' );

    // Declare the array
    $dropdownitems = array();

    // Add first & default value of the dropdown
    $dropdownitems[] = array(
      'text' => 'Default value of dropdown',
      'value' => ''
    );

    // Loop through objects
    foreach ($terms as $term) {
      $dropdownitems[] = array(
        'value' => $term->id,
        'text' => $term->name
      );
    }

    // Add terms as dropdown options
    foreach ( $form['fields'] as &$field ) {
      if ( $field->id == 1 ) {
        $field->choices = $dropdownitems;
      }
    }

    return $form;
    wp_reset_postdata();
  }
}

@zmayyem
Copy link

zmayyem commented Mar 8, 2021

Hello @cristacheda , thanks for your reply
yes yes this is work , but need to add this line before the modified code

$terms = get_the_terms( $post->ID, 'taxonomy_name' );

// Loop through objects
   foreach ($terms as $term) {
     $dropdownitems[] = array(
       'value' => $term->id,
       'text' => $term->name
     );
   }

Thanks again for your reply boss :)

@warengonzaga
Copy link

hey, @cristacheda a very useful code!

// add first & default value of the dropdown
$dropdownitems[] = array(
  'text' => 'Default value of dropdown',
  'value' => ''
);

These lines are safe to remove if you don't want to add any initial default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment