首页 视频课程 WordPress开发文档用户权限与角色 添加角色

添加角色

2020-04-28 / 3676阅

本视频需要登录以后才能播放

登录

如果现有的角色不能满足你的需求,你可以选择自己注册角色

add_role( $role,$display_name, $capabilities  =  array()); 

上面的删除可以用来添加一个角色,第一个是角色的别名,第二个是显示的名称,第三个是赋予的权限。

function xx__update_custom_roles()
{
       if (get_option('custom_roles_version') < 1) {
              add_role('vip', '新角色名称', array('read' => true, 'level_0' => true));
              update_option('custom_roles_version', 1);
       }
}
add_action('init', 'xx__update_custom_roles'); 

添加角色之前,可以判断一个角色是否存在,然后在做操作。

当插件启用时创建角色

function add_roles_on_plugin_activation() {
       add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 'level_0' => true ) );
}
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' ); 

创建角色,继承权限

add_role( 'superintendent', 'Superintendent', get_role( 'administrator' )->capabilities ); 

删除角色

remove_role( 'subscriber');  

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
我的见解
目录