Home › Forums › Bug Reports › Setting $field['default_value'] not working in acf/prepare_field
Hi Elliot,
I’m using ACF Forms and am trying to set the default value of a checkbox field (multiple checkboxes, some of them initially checked) through the filter acf/prepare_field
– it doesn’t have any effect though. It works through acf/load_field
, but I’m having some other issues there with query vars not being available, probably because it’s too early in the loop.
I think this could potentially be a bug – shouldn’t it be possible to set defaults via acf/prepare_field
?
Cheers,
Moritz
It should be possible to set the default value using acf/prepare field.
More information on the field and the code your using to try to set the default would help.
Hi @hube2,
currently, I’ve split up the code into setting the available choices for the checkboxes in acf/prepare_field
and defining the default value for the field in acf/load_field
. I hope my code below is enough to understand what I’m doing.
function wptd_acf_load_brot_verfuegbarkeit_values( $field ) {
if (!is_admin()) {
// Only in frontend
$verfuegbarkeiten = get_query_var('verfuegbarkeiten') ?? $_SESSION['verfuegbarkeiten'] ;
$sortiment_heute = get_query_var('sortiment_heute') ?? $_SESSION['sortiment_heute'];
if (!empty($sortiment_heute)) {
$field['default_value'] = wptd_get_brote_available($sortiment_heute, $verfuegbarkeiten);
}
}
return $field;
}
add_filter('acf/load_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_values');
function wptd_acf_load_brot_verfuegbarkeit_choices( $field ) {
if (!is_admin()) {
// Only in frontend
$sortiment_heute = get_query_var('sortiment_heute');
if (!empty($sortiment_heute)) {
$field['choices'] = array_reduce($sortiment_heute, function($result, $item) {
$result[$item] = get_the_title($item);
return $result;
}, []);
}
}
return $field;
}
add_filter('acf/prepare_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_choices');
Hi @hube2,
I have currently split my code for loading the choices for my checkboxes and for setting the default value. I hope my code gives you enough insight to understand what I’m trying to do.
function wptd_acf_load_brot_verfuegbarkeit_values( $field ) {
if (!is_admin()) {
// Only in frontend
$verfuegbarkeiten = get_query_var('verfuegbarkeiten') ?? $_SESSION['verfuegbarkeiten'] ;
$sortiment_heute = get_query_var('sortiment_heute') ?? $_SESSION['sortiment_heute'];
if (!empty($sortiment_heute)) {
$field['default_value'] = wptd_get_brote_available($sortiment_heute, $verfuegbarkeiten);
}
}
return $field;
}
add_filter('acf/load_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_values');
function wptd_acf_load_brot_verfuegbarkeit_choices( $field ) {
if (!is_admin()) {
// Only in frontend
$sortiment_heute = get_query_var('sortiment_heute');
if (!empty($sortiment_heute)) {
$field['choices'] = array_reduce($sortiment_heute, function($result, $item) {
$result[$item] = get_the_title($item);
return $result;
}, []);
}
}
return $field;
}
add_filter('acf/prepare_field/key=field_5eddd053baf5a', 'wptd_acf_load_brot_verfuegbarkeit_choices');
Cheers,
Moritz
Hey @hube2,
I just found out that I need to use $field['value']
instead of $field['default_value']
in acf/prepare_field
to do what I want to do! 🙂
Cheers,
Moritz
I also had some difficulty getting the $field['default_value']
to work in the acf/prepare_field
. @dreiqbik got me on track (thanks!). You can obviously use another filter, but if you need to define the default value when preparing the field, using $field['value']
can imitate the $field['default_value']
.
This will override any selected value though, so you’ll have to filter it yourself. Check if it already has a value set, and if you don’t know if the field allows for null, you should check for that too:
if(!$field['value'] && !$field['allow_null']) {
$field['value'] = 'YOUR DEFAULT VALUE';
}
The topic ‘Setting $field['default_value'] not working in acf/prepare_field’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.