get_editable_roles()
云策文档标注
概述
get_editable_roles() 函数用于获取当前用户可编辑的用户角色列表,通过过滤 $wp_roles 对象中的角色,确保插件能根据情境或用户权限移除不合适的角色,防止非管理员用户不当分配管理员权限。
关键要点
- 函数返回一个包含角色信息的数组数组,用于限制用户可分配的角色。
- 通过 editable_roles 过滤器实现角色列表的过滤,允许管理员委托用户管理权限。
- 该函数定义在 wp-admin/includes/user.php 文件中,仅在管理后台部分加载。
- 相关函数包括 wp_roles() 和 apply_filters(),用于获取全局 WP_Roles 实例和应用过滤器。
代码示例
function get_editable_roles() {
$all_roles = wp_roles()->roles;
/**
* Filters the list of editable roles.
*
* @since 2.8.0
*
* @param array[] $all_roles Array of arrays containing role information.
*/
$editable_roles = apply_filters( 'editable_roles', $all_roles );
return $editable_roles;
}
原文内容
Fetch a filtered list of user roles that the current user is allowed to edit.
Description
Simple function whose main purpose is to allow filtering of the list of roles in the $wp_roles object so that plugins can remove inappropriate ones depending on the situation or user making edits.
Specifically because without filtering anyone with the edit_users capability can edit others to be administrators, even if they are only editors or authors. This filter allows admins to delegate user management.
Source
function get_editable_roles() {
$all_roles = wp_roles()->roles;
/**
* Filters the list of editable roles.
*
* @since 2.8.0
*
* @param array[] $all_roles Array of arrays containing role information.
*/
$editable_roles = apply_filters( 'editable_roles', $all_roles );
return $editable_roles;
}
Hooks
- apply_filters( ‘editable_roles’, array[] $all_roles )
-
Filters the list of editable roles.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Codex
Example
Currently, you can assign the following roles:
<dl> $role_info): ?> <dt></dt> <dd> <ul> $_): ?> <li></li> </ul> </dd> </dl>