Support

Account

Home Forums Add-ons Repeater Field Creating default subfields in repeater field Reply To: Creating default subfields in repeater field

  • What I am actually doing is just loading default subfields (with no values) into a repeater. Just empty subfields the user can field out.

    So basically, I am pre-adding subfields to a repeater, so I do not have to add the fields manually.

    The subfields are created automatically using acf/load_field/type=flexible_content.

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text'
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Now, this works perfectly. Keep in mind – doing it this way does not allow you to add child fields via ACF. You will have to add it within the same acf/load_field/type=repeater hook.

    I believe you want to add default values – which you can do like this:

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text',
          'default_value' => 'Hi' // ADD THIS LINE
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Test it out in functions.php if that is what you are trying to achieve.