Home › Forums › General Issues › Populate select field from options text area with value : label › Reply To: Populate select field from options text area with value : label
Using that example, what you need to do is go one step further
function acf_load_color_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_field('my_select_values', 'option', false);
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices)
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
// split each line on ' : '
$parts = explode(' : ', $choice);
// make sure we have 2 parts
if (count($parts) > 1) {
$value = $parts[0];
$label = $parts[1];
} else {
// no separator found
$value = $choice;
$label = $choice;
}
$field['choices'][$value] = $label;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');
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.