以下代码创建一个新角色。事实上,它只是克隆订阅者角色,所以如果您当前没有使用该角色,那么您也可以使用它。
以下函数只需运行一次(当用户是“管理端”时运行)
//Creates a custom role called \'my_new_role\'.
//Run this once then feel free to comment out the next line:
add_action(\'admin_init\', \'my_custom_role\');
function my_custom_role(){
global $wp_roles;
$subscriber = $wp_roles->get_role(\'subscriber\');
//Adding a \'new_role\' with subscriber caps
$wp_roles->add_role(\'my_new_role\', \'My New Role\', $subscriber->capabilities);
//Optional add/remove caps, like the capability to access the dashboard
//$wp_roles->remove_cap(\'my_new_role\',\'read\');
}
参见法典
more information on capabilities.
无论您在哪里处理表单,都需要创建一个新用户,并为他们分配角色“my\\u new\\u role”。要执行此操作,请使用wp_insert_user
. 举个简单的例子:
wp_insert_user( array (
\'user_login\' => \'JoeBloggs\',
\'user_pass\' => \'a_password_43463\',
\'first_name\' => \'Joseph\',
\'last_name\' => \'Bloggs\',
\'role\'=>\'my_new_role\') ) ;
Prior to the above you should have performed all the nonce-checks, data validation and any other checks.
您可能希望在这些新用户登录时将其重定向到其他页面(而不是仪表板)。要执行此操作,请使用login_redirect
滤器
add_filter(\'login_redirect\', \'dashboard_redirect\');
function dashboard_redirect($url) {
global $current_user;
get_currentuserinfo();
if (current_user_can(\'my_new_role\')) {
//current user is \'my_new_role\', take them somewhere else...
$url = home_url();
}
return $url;
}