Last active
August 29, 2023 07:51
-
-
Save joshuacerbito/3968bf9eaacd44a64c05075afab84697 to your computer and use it in GitHub Desktop.
Pre-Fill Wordpress ACF Repeater Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.