Support

Account

Home Forums Add-ons Options Page Using acf/load_field to populate values? Reply To: Using acf/load_field to populate values?

  • Hi John,

    Normally, I was following field keys starting with ‘field_’ string for being orderly, but I didn’t know that it was important for avoiding problems. Anyway I will pay extra attention to that point, thanks.

    As for loading values for the dynamic form, I have been using acf/load_field filter in a loop and using default_values field to insert the values from the third party API. So when the form is initialized fields are populated with default values. After that user makes necessary changed if needed, saves the form, and new values gets stored to database. In a way problem looks solved for now. (But I haved noted that I need to use acf_update_field($field) before returning $field in order to get default_value to stored to DB)

    Please let me know is this a bad practice. I am doing something similar to this:

    $new_fields = array(
      array(
        'key' => 'field_product_item_title' . $item_id,
        'name' => 'field_product_item_title' . $item_id,
        '_name' => 'field_product_item_title' . $item_id,
        'label' => 'Product Title',
        'type' => 'text',
        'default_value' => $title_from_third_party_api
        //... (instructions, conditional_logic, required etc)
      ),
      //... add rest of the field items
    );
    
    foreach($new_fields as $index=>$new_field){
      array_push($field['sub_fields'], $new_field);
    }
    
    acf_update_field($field);
    return $field;
    

    Thanks!