角色下拉列表,link角色名称到ID

Dropdown list of roles, link role name to ID

我想制作一个特定角色的下拉列表,并为每个角色名称设置一个值。

目前我可以显示所有角色,但不能显示在下拉列表中,我想删除默认角色。

编辑

<?php if ( ! function_exists( 'get_editable_roles' ) ) {
  require_once ABSPATH . 'wp-admin/includes/user.php';
}
?>
<select id="soflow" name="roles">
<?php $roles = get_editable_roles();
  foreach ($roles as $key => $value) {
  echo '<option value=".$value->name.">'.$value->name.'</option>';            }
 ?>
</select>

这个甚至 return 一个角色名称我哪里弄错了?

您可以创建自定义函数并调用 functions.php 文件。代码如下,

<?php
function getUsersByRole($role,$name,$selected = '',$extra = '') {
 global $wpdb;

 $wp_user_search = new WP_User_Query(array("role"=> $role));
   $role_data = $wp_user_search->get_results();
  foreach($role_data  as $item){
   $role_data_ids[] = $item->ID;
  }

 $ids = implode(',', $role_data_ids);
 $r = $wpdb->get_results("SELECT *   from ".$wpdb->prefix . "users where id IN(".$ids .")", ARRAY_A);

 $content .='<select name="'.$name.'" id="'.$name.'" '.$extra.'>';

 if($selected == ''){
  $content .='<option value="" selected="selected">Choose a user</option>';
 }else{
   $r_user = $wpdb->get_results("SELECT *   from ".$wpdb->prefix . "users where ID = ".$selected."", ARRAY_A);
   $content .='<option value="'.$selected.'" selected="selected">'.stripslashes($r_user[0]['display_name']).'</option>';
 }

 for($i=0; $i<count($r); $i++){     
 $content .='<option value="'.$r[$i]['ID'].'">'.stripslashes($r[$i]['display_name']).'</option>';
 }
 $content .='</select>';

 return $content;
}
?>

现在,您可以将此功能放在.php文件的页面或自定义页面上

echo getUsersByRole('subscriber','user_id', $r[0]['user_id'],'onchange="dosomething()"');
 
// or just use which will not set a default selected user or any extra attributes
echo getUsersByRole('subscriber','user_id');

谢谢!!

如果我理解正确的话,你需要这个:

<?php if ( ! function_exists( 'get_editable_roles' ) ) {
    require_once ABSPATH . 'wp-admin/includes/user.php';
}
?>
    <select id="soflow" name="roles">
        <?php
            foreach (get_editable_roles() as $role_name => $role_info) {
                echo '<option vlaue=".role_name.">'.role_name.'</option>'
            }
        ?>
    </select>

你可以用 css 设置样式:

select#soflow {
   -webkit-appearance: button;
   -webkit-border-radius: 2px;
   -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
   -webkit-padding-end: 20px;
   -webkit-padding-start: 2px;
   -webkit-user-select: none;
   background-image: url(http://i62.tinypic.com/15xvbd5.png), -webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
   background-position: 97% center;
   background-repeat: no-repeat;
   border: 1px solid #AAA;
   color: #555;
   font-size: inherit;
   margin: 20px;
   overflow: hidden;
   padding: 5px 10px;
   text-overflow: ellipsis;
   white-space: nowrap;
   width: 300px;
}

现在使用此代码我获得了角色列表以及数组

<select id="soflow" name="roles">
<?php $roles_name = get_editable_roles();
foreach ($roles_names as $roles) {
foreach ($roles as $value->name) {
echo '<option value=".$value>name.">' . $value->name . '</option>';
}
}?>
</select>

答案在这里。

<select id="roles" name="roles" class="fre-chosen-single">
  <?php
     foreach (get_editable_roles() as  $role_name => $role_info) {
       echo '<option value="'.$role_name.'">' . $role_info['name'] . '</option>';
     }?>
</select>

如何筛选角色? 是否可以为角色添加 slug?