Home › Forums › General Issues › Setting a default for an image field. › Reply To: Setting a default for an image field.
So I will answer my own question. For image and taxonomy fields, the way to set a default value is programmatically using add_filter for acf/load_value. In both cases, the filter function returns an ID value, the ID of the image or the ID of the term.
function gci_acf_load_value_graphic($value, $postid, $field) {
$retval = $value;
// We only do this for fields named "graphic".
if ($field['_name'] != 'graphic') {
return($retval);
}
$flds = ['fb1_graphic', 'fb2_graphic', 'fb3_graphic', 'fb4_graphic'];
if (in_array($field['name'], $flds)) {
if (!$value) {
$img = gci_acf_get_default_graphic($field['name']);
$retval = $img['ID'];
}
}
return($retval);
}
add_filter('acf/load_value/type=image', 'gci_acf_load_value_graphic', 11, 3);
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.