Support

Account

Home Forums General Issues Add Choices to Select Menu Reply To: Add Choices to Select Menu

  • This functionality has recently been added to the checkbox field, it was originally only part of the radio field. I imagine that eventually it will find it’s way into the select field as well.

    In the mean time, a work-a-round

    Create your select field that has a choice of “Other”
    Create a text field with conditional logic to show if “Other” is selected for the choice.

    That’s the easy bit.

    For the saving of the new value as a choice I would recommend looking at the code that does this for the checkbox field

    
    // FROM advanced-custom-fields-pro/fields/checkbox.php LINE 368 
    
    // save_other_choice
    if( $field['save_custom'] ) {
      
      // get raw $field (may have been changed via repeater field)
      // if field is local, it won't have an ID
      $selector = $field['ID'] ? $field['ID'] : $field['key'];
      $field = acf_get_field( $selector, true );
      
      
      // bail early if no ID (JSON only)
      if( !$field['ID'] ) return $value;
      
      
      // loop
      foreach( $value as $v ) {
        
        // ignore if already eixsts
        if( isset($field['choices'][ $v ]) ) continue;
        
        
        // unslash (fixes serialize single quote issue)
        $v = wp_unslash($v);
        
        
        // append
        $field['choices'][ $v ] = $v;
        
      }
      
      
      // save
      acf_update_field( $field );
      
    }    
    

    basically, you need to get the current field, update the choices with your addition and then call acf_update_field().