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


Using acf/load_field to populate values?

  • Hi,

    I want to populate an options page form fields with data from 3rd party service when a user hits save at the options page.I am using acf_add_local_field_group to add repeater fields dynamically. Now this is the only approach I was able to come up with to modify form fields labels etc:

    add_filter('acf/load_field/key=my_group_item_1', 'populate_fields', 20, 3);

    function populate_fields($field){
    $field['sub_fields'][1] = array(
      'key' => 'subfield_key_01',
      'name' => 'subfield_name_01',
      'label'=> 'Enter title'
    return $field;

    With this approach I can update fields labels etc but not the values. When user hits the ‘save’ empty value to gets recoreded to DB, in this case I want to override empty value and populate field values with default data from the service. I hope someone can help me out with options page dynamic form fields experience.


  • First, before you run into other issues, your field keys must start with "field_". You will have issues with using "subfield_". This is in the documentation, but not prominent.

    What you want to do is use an acf/load_value filter applied to the repeater. First test to see if the value is empty. If the value is an array then values have already been saved to the field. If the value is empty then nothing has been saved yet and you can populate the repeater.

    To populate the repeater with defaults you need to construct an array for the repeater values using the field keys.

    $repeater = array(
      // nested array for each row
        // field key => value pairs for each sub field
        'field_12345' => 'value for the sub field',
        'field_34567' => 'value for the sub field'
      // start next row

    Let me know if you have any questions.

  • 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(
        '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);
    return $field;


  • Repeaters work differently, they do not have default values. Each sub field has a default value. Load field filters for sub fields are only called once, meaning that all of the rows will have the same default value. There isn’t any way to have a different default value for every row.

    Using the default value for most fields is probably a good bet. It just won’t work with sub fields.

  • Well in my case I am using acf/load_field to load form fields dynamically for each post in a loop, similar to this example:

    if( $loop->have_posts() ):
        while( $loop->have_posts() ): $loop->the_post();
        $id = get_the_ID();
        if( function_exists('acf_add_local_field_group') ):
          'key' => 'field_product_item_' . $id,
          'title' => 'Product Item #' . $id,
          'fields' => array(
              'key' => 'field_product_item_group_' . $id,
    	  'type' => 'group',
              'sub_fields' => array()
               //... dynamic items with default values will be added to this sub_field using acf/load_field
        add_filter('acf/load_field/key=field_product_item_group_' . $id, 'load_product_details_form', 20, 3);

    So this approach seems to work so far. Anyway I hope this is not prone to problems in the future.

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

The topic ‘Using acf/load_field to populate values?’ is closed to new replies.