Home › Forums › Add-ons › Repeater Field › Default open fields in repeater › Reply To: Default open fields in repeater
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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.