本文目錄1修改用戶角色名稱2新增用戶角色3參考與拓展
WordPress 自帶瞭多個默認的用戶角色,比如 超級管理員(多站點)、管理員、編輯、作者、投稿者、訂閱者。在實際使用中,如果我們需要更改這些默認的用戶角色名稱,或者添加新的用戶角色,該如何操作呢?今天就來說說說通過代碼實現的方式。
修改用戶角色名稱
將下面的代碼添加到當前主題的 functions.php ,可以將 administrator 這個角色名修改為 Owner,你可以根據實際需要照樣畫葫蘆就可以瞭。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * WordPress 修改用戶角色名稱和添加新用戶角色 * https://www.wpdaxue.com/change-wordpress-role-name-add-new-role.html */ function wpdx_change_role_name() { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); //你可以像這樣所列出當前所有有效的用戶角色... //$roles = $wp_roles->get_names(); //print_r($roles); //你可以使用其他角色名稱替換 "administrator" , "editor", "author", "contributor" 或 "subscriber"... $wp_roles->roles['administrator']['name'] = 'Owner'; $wp_roles->role_names['administrator'] = 'Owner'; } add_action('init', 'wpdx_change_role_name'); |
/**
* WordPress 修改用戶角色名稱和添加新用戶角色
* https://www.wpdaxue.com/change-wordpress-role-name-add-new-role.html
*/
function wpdx_change_role_name() {
global $wp_roles; if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles(); //你可以像這樣所列出當前所有有效的用戶角色…
//$roles = $wp_roles->get_names();
//print_r($roles); //你可以使用其他角色名稱替換 "administrator" , "editor", "author", "contributor" 或 "subscriber"…
$wp_roles->roles[‘administrator’][‘name’] = ‘Owner’;
$wp_roles->role_names[‘administrator’] = ‘Owner’;
}
add_action(‘init’, ‘wpdx_change_role_name’);
新增用戶角色
將下面的代碼添加到當前主題的 functions.php ,就可以添加一個名為 Basic Contributor 的用戶角色
1 2 3 4 5 |
add_role('basic_contributor', 'Basic Contributor', array( 'read' => true, // 使用 true 表示包含這個權限 'edit_posts' => true, 'delete_posts' => false, // 使用 false 表示不包含這個權限 )); |
add_role(‘basic_contributor’, ‘Basic Contributor’, array(
‘read’ => true, // 使用 true 表示包含這個權限
‘edit_posts’ => true,
‘delete_posts’ => false, // 使用 false 表示不包含這個權限
));
參考與拓展
add_role() 函數:http://codex.wordpress.org/Function_Reference/add_role
WordPress用戶角色與用戶能力/權限
WordPress用戶角色編輯外掛:User Role Editor