Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Last active August 29, 2023 07:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuacerbito/3968bf9eaacd44a64c05075afab84697 to your computer and use it in GitHub Desktop.
Save joshuacerbito/3968bf9eaacd44a64c05075afab84697 to your computer and use it in GitHub Desktop.
Pre-Fill Wordpress ACF Repeater Field
<?php
// Pre-fills a repeater field (and its sub-fields) with default values
// - assuming we have a Repeater field with the key "repeater_field_123"
// - with the sub-fields "sub_field_1" and "sub_field_2"
function prefill_repeater( $field ) {
if ($field['value'] === false) {
$field['value'] = array(
array(
'sub_field_1' => 'My sub field 1 default content',
'sub_field_2' => 'My sub field 2 default content',
),
array(
'sub_field_1' => 'And another one',
'sub_field_2' => 'And yet another one',
),
);
}
return $field;
}
add_filter('acf/prepare_field/key=repeater_field_123', 'prefill_repeater');
@arneprescher
Copy link

Thank you so much, this is fantastic. Had to search a long time for a solution that works! But for it to really work for me, I had to check $field[value] against empty(), since a newly instantiated repeater field is probably an empty array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment