【问题标题】:Dropdown list of roles, link role name to ID角色下拉列表,将角色名称链接到 ID
【发布时间】:2019-08-08 15:04:46
【问题描述】:

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

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

编辑

<?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>

这个甚至没有返回角色名称我哪里弄错了?

【问题讨论】:

    标签: wordpress roles


    【解决方案1】:

    您可以创建自定义函数并调用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');

    谢谢!!

    【讨论】:

    • 此功能将某个角色的用户列表到下拉列表中。
    【解决方案2】:

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

    <?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;
    }
    

    【讨论】:

    • 越来越近了。但我在下拉列表中没有角色,而是“role_name”。
    【解决方案3】:

    现在通过这段代码,我获得了角色列表和数组

    <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>
    

    【讨论】:

      【解决方案4】:

      这就是答案。

      <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 ?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-06
        • 2021-11-02
        • 2020-08-06
        • 1970-01-01
        • 2016-09-09
        • 2019-07-11
        • 2022-01-15
        • 2012-10-08
        相关资源
        最近更新 更多