Support

Account

Home Forums Front-end Issues Pre-Populate GF form with ACF Field Data

Solved

Pre-Populate GF form with ACF Field Data

  • Hi Everyone,

    I am working on getting ACF data (specific to a logged in user) to appear pre-populated in Gravity Forms, I tried using this code from a 4 year old post in GF Forums but no luck.

    https://www.gravityhelp.com/forums/topic/dynamically-populate-advanced-custom-field-instead-of-a-normal-custom-field

    /* Dynamically populate Gravity
    ———————————— */
    add_filter(‘gform_field_value_dept_name’, ‘my_user_info’);
    function my_user_info($value){
    return get_field(‘dept_name’);
    }

    I added ACF Fields for “Logged In” users and have 3 fields (Dept Name, Dept ID and Billing Link), once I get the first piece of code working I will duplicate it (unless anyone has a better solution to consolidate it to one line of code, I am still learning PHP functions.

    Any ideas/solutions?

    Thanks in advance!
    Ken

  • As long as GF is still working the way it worked in that post. I’m not very familiar with GF, I can help you get things out of ACF but putting them somewhere else…

    When getting the value for a user you need to give ACF the $post_id argument.

    
    add_filter('gform_field_value_dept_name', 'my_user_info');
    function my_user_info($value){
      $id = get_current_user_id();
      $post_id = 'user_'.$id;
      return get_field('dept_name', $post_id);
    }
    

    see getting values from other places on this page http://www.advancedcustomfields.com/resources/get_field/

  • Hi John,

    That code worked perfectly! Now 2nd part of the question, is best solution to repeat the code or is there a way to have one function return all three values?

    Thanks for quick reply!

    Ken

  • If all of your fields in GF have the same field name as the field names in ACF then there is a way that you can use a single function, that’s if I’m understanding the GF hook from what I’m seeing.

    I’m assuming that the GF hook is "gform_field_value_{$your_field_name}"

    Lets say you have 2 fields

    
    $field_names = array('field_1', 'field_2');
    foreach ($field_names as $name) {
      add_filter('gform_field_value_'.$name, 'load_my_custom_values');
    }
    function load_my_custom_values($value) {
      // get the current filter that was called
      // it will contain the field name
      $filter = current_filter();
      // remove 'gform_field_value_' from filter name
      $field = str_replace('gform_field_value_', '', $filter);
      $id = get_current_user_id();
      $post_id = 'user_'.$id;
      $value = get_field($field, $post_id);
      return $value;
    }
    
  • Thanks again John!

    I modified the second code example to have a third variable, and it worked perfectly!! I then modified a value and refreshed the page and it brought it in as I want!

    This is great!

    Thanks again this helps me out a lot! (and good code example to follow!)

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.