Support

Account

Home Forums Add-ons Repeater Field Default open fields in repeater

Solving

Default open fields in repeater

  • Does anyone know if there’s a way to set the default number of open repeater rows?

    We have a client who enters rows of titles and links. They add maybe a hundred to each post and have expressed that they would like to have like 20 blank fields open by default because clicking the little plus icon is apparently too much work.

    Is this something that is even possible in ACF?

  • This will create a number of empty rows when there are no rows

    
    function set_start_rows($value, $post_id, $field) {
      if (!$value) {
        $row = array(
          // field key => value pairs for all subfields
          'field_5bcf70b75df1d' => NULL,
        );
        $number_of_rows = 10;
        $value = array_fill(0, $number_of_rows, $row);
      }
      return $value;
    }
    add_filter('acf/load_value/key=field_5bcf70a95df1c', 'set_start_rows', 20, 3);
    

    This would automatically add 10 a number of new rows if there are no rows or the last row does not contain a value in a field

    
    function set_start_rows($value, $post_id, $field) {
      // only do this in the admin
      if (!is_admin()) {
        return $value;
      }
      $add_rows = false;
      $index = 0;
      if (is_array($value)) {
        $index = count($value);
        if ($value[count($value)-1]['field_5bcf70b75df1d']) {
          // field is not empty in last row
          $add_rows = true;
        }
      }
      if (!$value) {
        $add_rows = true;
      }
      if ($add_rows) {
        if (!$value) {
          $value = array();
        }
        $row = array(
          // field key => value pairs for all subfields
          'field_5bcf70b75df1d' => NULL,
        );
        $number_of_rows = 5;
        $value = array_merge($value, array_fill($index, $number_of_rows, $row));
      }
      return $value;
    }
    add_filter('acf/load_value/key=field_5bcf70a95df1c', 'set_start_rows', 20, 3);
    

    A really elegant way to do this would be to use JavaScript and detect when a value is added to the last row and then automatically add a new row. This should be possible, but I don’t have the answer to how to do that and it would take me a long time to figure out and test.

  • I tried plugging this in to functions.php and set the field value to the post_name field in wp_posts table. That value is field_5b872b6587fbe

    I assumed replacing the field in your example was referencing this field from wp_posts.

    My admin panel is not doing anything differently when I test it so I figured I’d explain what I did and see if you have feedback.

    I should also clarify that I put a die(‘test’); in the set_start_rows function right at the beginning and it did not trigger. looks like the function isnt being called. Could this have a typo or something in it?

    add_filter(‘acf/load_value/key=field_5b872b6587fbe’, ‘set_start_rows’, 20, 3);

  • You need to change the field key in the hook

    
    .......add_filter('acf/load_value/key=field_5bcf70a95df1c',..............
    

    with the field key of your repeater. Then you need to change the field key where we see if there is any value with the field key of your sub field if you’re doing anything with.

    
    .............if ($value[count($value)-1]['field_5bcf70b75df1d']) {........
    
  • Yes, I assumed that. The field is set accordingly.

    I got the key field by checking phpmyadmin in the wp_posts table where the post_title was the name of my field and post_name is the field you refer to.

    Yet it does not trigger the function.

    If this is not how you obtain the key then some clarification may be necessary.

  • You can get the field keys by editing the field group and checking the box for them under screen options. I actually tried this a dev site and it works for me. I can’t tell what’s causing the issue on your end.

    What code did you use?

    What is the field key of your repeater?

    If you’re trying the second code, what is the field key of the sub field you’re checking?

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

The topic ‘Default open fields in repeater’ is closed to new replies.