Dynamic Array Options in WordPress Plugin Settings Class

For one of my plugins I need to add a multiselect to my plugin options, but the options in the multiselect are dynamic (autogenerated from the given site’s user roles).

    $this->settings['manageraccess'] = array(
        'section' => 'manager',
        'title'   => __('Manager Mode Access'),
        'desc'    => __(''),
        'type'    => 'manageraccess',
        'std'     => '',
        'dflt'    => '',
        'helplink'=> 'yes',
        'submit' => 'yes'                   
    );

Normally I would add:

Read More
'choices' => array(
    'op1' => 'Option 1',
    'op2' => 'Option 2',
),

to the above array, but I need to generate them dynamically, like I do in my select box. My field case looks like this:

        case 'manageraccess':
            global $wp_roles;
            $all_roles = $wp_roles->roles; 
            $editable_roles = apply_filters('editable_roles', $all_roles); 
            echo '<select multiple="multiple" class="select chozed-select' . $field_class . '" name="myplugin_options[' . $id . '][]" data-placeholder="&nbsp;">';
            foreach($editable_roles as $role=>$theroles){
                echo '<option value="' . esc_attr($role) . '" '.selected($options[$id], $role, false) . '>'.$wp_roles->role_names[$role].'</option>';
            }
            echo '</select>' . $helplink;
            if ($desc != '')
            echo '<br /><div class="ssfa-description">' . $desc . '</div>'; 
            if ($submit != null)
            echo '<br /><br /><br />'.$submit;                  
        break;          

Related posts

Leave a Reply

1 comment

  1. Well I figured it out.

    public function get_settings(){
        global $wp_roles;
    
        $this->settings['managertest'] = array(
            'section' => 'manager',
            'title'   => __('Manager Mode Access'),
            'desc'    => __(''),
            'type'    => 'multi_select',
            'std'     => '',
            'dflt'    => '',
            'choices' => $wp_roles->role_names,
            'helplink'=> 'yes',
            'submit' => 'yes'                   
        );