Support

Account

Home Forums General Issues Populate select field from options text area with value : label

Solving

Populate select field from options text area with value : label

  • I would like to populate a select field from an options textfield where the label is visible in the select field but the value is returned. The reason is so the values/labels can be changed site wide. I could probably do it with some CSS but I wanted to explore this option first.

    Specifically, I’d like to have an options text area with some colors (bkg_color_textfield), like this:

    #000 : Black
    #333 : Very Dark Gray
    #666 : Dark Gray

    Have the select field (bkg_color) look like this:

    Black
    Very Dark Gray
    Dark Gray

    And have the value: #000, #333, or #666 go into the rendered page.

    the_sub_field(‘bkg_color’)

    What gets returned now is the whole string (#000 : Black) instead of just the hex color (#000).

  • I’m not sure I understand completely, do you already have a select field that is being dynamically populated from the text field? I guess I’m missing how your currently getting the value of #000 : Black.

  • Yes. The select field is populated from an options text area like Example 1 on https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/.

  • 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');
    
  • You’re amazing. It worked perfectly. Thanks!

    One note… do you need a ; after $choices = explode(“\n”, $choices) ?

  • sorry about the typo, don’t know how I managed to delete that semi colon. Glad you got it working.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Populate select field from options text area with value : label’ is closed to new replies.