Hi,
I am trying to get a default value for datepicker, basically if field abc has no value, then it should have value xyz. If the field has value, then the value will be displayed. I have tried it with several solutions but I am no expert:
add_filter('acf/load_field/name=date_of_birth', function ($field) {
$field['default_value'] = date('d.m.Y');
return $field;
});
or
$goempty = '---';
$defaultdateofbirth = get_field('date_of_birth');
if (empty($defaultdateofbirth)) {
update_field('date_of_birth', '---');
} else {
update_field('date_of_birth', $goempty);
}
The second one adds the today’s date and overwrites everything else.
Can someone help me figure out a solution for that? Thanks!!
Has really no one an idea how to solcve this? 🙁
If you want to set the default value of a date field when loading a field it must be in the same format that ACF stores in the DB
add_filter('acf/load_field/name=date_of_birth', function ($field) {
$field['default_value'] = date('Ymd');
return $field;
});
I can now confirm John’s solution above is perfect and does the trick like a charm !