Support

Account

Home Forums General Issues Generate select options like radio options

Unread

Generate select options like radio options

  • Hi,

    I’ve been trying to dynamically generate some of my select box options. See. I need a select box, that has some values (employee names) but that you can add other name to the list if this one isn’t here. Preferably whitout reloading the form or loosing whatever data I have entered.

    I tried to work with the function shown in the docs:

    
    function populate_select_field( $field ) {
      // reset choices
      // $field['choices'] = array();
    
      // load repeater from the options page
      if( get_field( 'add_electrician', 110 ) ) {
    
        while( has_sub_field( 'add_electrician', 110 ) ) {
          $value = get_sub_field('full_name');
          $label = get_sub_field('full_name');
    
          $field['choices'][ $value ] = $label;
        }
      }
    
      // Important: return the field
      return $field;
    }
    
    add_filter('acf/load_field/name=ev_employees', 'populate_select_field');
    

    This works as long as I save the employee names in the same post/page/options page, because the ID is a constant. So, I would have to save the employee name on a different form first in order to populate the select box.

    I would like to have a select box that works like the radio field, where you have an OTHER option which you can fill and it gets added to the options that already exists.

Viewing 1 post (of 1 total)

The topic ‘Generate select options like radio options’ is closed to new replies.