对于我的一个插件,我需要在插件选项中添加多选,但多选中的选项是动态的(从给定网站的用户角色自动生成)。
$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=" ">';
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;
答案 0 :(得分: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'
);