Support

Account

Home Forums General Issues Dynamically populate select field with value/label pair, not just value! Reply To: Dynamically populate select field with value/label pair, not just value!

  • Found the solution:

    function acf_load_icon_field_choices( $field ) {
        // reset choices
        $field['choices'] = array();
        // get the textarea value from options page without any formatting
        $choices = get_field('icon_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 ) {
                $exploded = explode(' : ', $choice);
                $field['choices'][ $exploded[0] ] = $exploded[1];
              //  $field['choices'][ $choice ] = $choice;
            }
        }
        // return the field
        return $field;
    }
    add_filter('acf/load_field/name=left_box_icon', 'acf_load_icon_field_choices');