2014-03-19 61 views
2

對於我的一個插件,我需要爲我的插件選項添加多選,但多選中的選項是動態的(從給定站點的用戶角色自動生成)。WordPress插件設置中的動態數組選項設置類

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

通常我會補充:

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

上述數組,但我需要動態生成它們,就像我在選擇框中做。我的現場案例如下所示:

 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;   

回答

2

嗯,我想通了。

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'     
    );