Support

Account

Home Forums Backend Issues (wp-admin) Dynamic population using shortcode ACF+Gravity Forms

Solved

Dynamic population using shortcode ACF+Gravity Forms

  • Hello,
    I’m using the gravity forms with acf and i need to populate a simple gravity forms field with a acf field data it’s pretty simple what i want, but is not working, is dynamic population using shortcode but i cant use a shortcode inside another shortcode, what i need is:

    [gravityforms id=1 field_values=’codigo_pacote=[acf field="codigo_pacote"]‘]

    it doesn’t work, i don’t know what i can do anymore.

    Thanks.

  • +1 We’d also love to dynamically populate the to: field of a Gravity form from an ACF field

  • Hello Buddies,

    It took me HOURS, and i don’t get every line of code i used, but I just coded what may make your day !

    WHAT I WANTED : I wanted to dynamically populate certain fields from a gravity form, using values from repeater subfields i had on an option page.

    So, here it is :

    add_filter("gform_pre_render_ID", "dynamic_populate");
    // Update the 'ID' to the ID of your form (You have it in URL while modifying the form). This hook happens each time your form is loaded (Pre_render).
    
    function dynamic_populate($form) {
        foreach($form['fields'] as &$field){
        //At this point, function will run for each field in my Gravity Form, but right after i add a condition, see what's next.
         
            if(strpos($field['cssClass'], 'dynamicfield') === false)
                continue;
            //Condition : each field which has the class dynamicfield (example). If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form. PS: You can add whichever condition you want. Example : Run only on select fields, run only on select fields AND classname...
            
            global $wpdb;
            $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT *
                FROM $wpdb->options
                WHERE option_name LIKE 'REPEATERNAME_%%_SUBFIELDNAME'
                "
            ));
            //Call WP Database and SELECT all data FROM the table you want, for me it was option table as my repeater is on an option page. Then find each rows WHERE option_name value is like 'REPEATERNAME_%%_SUBFIELDNAME'. To get this, read the data naming convention part in here : http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/.
    
             //If your repeater isn't on an option page, you might want to call FROM wp_postmeta WHERE meta_key LIKE 'REPEATERNAME_%%_SUBFIELDNAME'.
            
            $choices = array(array('text' => 'Choisissez une base', 'value' => ' '));
            //Default choice for my dynamic select field. You shall not use any value, so if it's required it won't validate.
            
            if( $rows ){
                foreach( $rows as $row ) {
                //If there are results, for each result, find the 'repeater row number'
                    preg_match('_([0-9]+)_', $row->option_name, $matches);
                    //Note that option_name is the name of the column from my option database, you might want to change it.
    
                    $name = 'REPEATERNAME_' . $matches[0] . '_SUBFIELDNAME';
                    $value = get_option($name);
                    //Get the subfield value from the row.
    
                    $choices[] = array('text' => $value, 'value' => $value);
                    //Create an option array for each subfield value.
                }  
            }
    
            $field['choices'] = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }

    I hope this is quite clear cause i’m not a PHP expert, and i’m also french so maybe my english isn’t that good.

    Tell me if it worked for you, and if you were looking after it.

    PS: Eliott, thanks for the powerful ACF Plug-in, i hope that solution will help i think it’s quite useful and powerful ! Or maybe i did a hell of a job for nothing that much interesting haha !

    Have a great day all!

    Check my sources :
    Dynamic populate Gravity Forms fields before Form pre-render => http://www.gravityhelp.com/documentation/gravity-forms/knowledge-base/tutorials/dynamically-populating-drop-down-fields/
    Querying the database for subfields values => http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/

  • Thanks sidoine.noungui, it was much more than what i need hahaha
    I solve this using do_shortcode. Sorry for this huge delay.

    Thanks.

  • In my case I wanted the form repeated for each organization (ultimately would appear in a pop-up) with the respective organization’s email populating a hidden “Send To” field.

    <?php if( have_rows(‘add_contact’) ): ?>

      <?php while( have_rows(‘add_contact’) ): the_row();?>

    • <?php the_sub_field(‘organization_name’); ?>
      <?php $email = get_sub_field(‘send_to_email’);?>
      <?php echo do_shortcode(‘[gravityforms id=1 title=false description=false field_values=get_email=’.$email.’]’); ?>
    • <?php endwhile; ?>

    <?php endif; ?>

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.