有时候,我们需要把bbpress里的特殊用户分个组,赋予特殊的权限,下面一段代码可以为bbpress增加一个用户角色,在编辑用户的profile的时候,将该用户变更为这个角色即可。代码放在functions.php里。
/************************************************************** code to add new role senior_participate to bbpress 为bbpress增加一个新的用户角色,方便特殊用户角色的权限设置 **************************************************************/ function add_new_roles( $bbp_roles ) { /* Add a role called senior_participate */ $bbp_roles['bbp_senior_participate'] = array( 'name' => 'Senior_participate', 'capabilities' => custom_capabilities( 'bbp_senior_participate' ) ); return $bbp_roles; } add_filter( 'bbp_get_dynamic_roles', 'add_new_roles', 1 ); function add_role_caps_filter( $caps, $role ) { /* Only filter for roles we are interested in! */ if( $role == 'bbp_senior_participate' ) $caps = custom_capabilities( $role ); return $caps; } add_filter( 'bbp_get_caps_for_role', 'add_role_caps_filter', 10, 2 ); function custom_capabilities( $role ) { switch ( $role ) { /* Capabilities for 'senior_participate' role */ case 'bbp_senior_participate': return array( // Primary caps 'spectate' => true, 'participate' => true, 'moderate' => false, 'throttle' => false, 'view_trash' => false, // Forum caps 'publish_forums' => false, 'edit_forums' => false, 'edit_others_forums' => false, 'delete_forums' => false, 'delete_others_forums' => false, 'read_private_forums' => true, 'read_hidden_forums' => false, // Topic caps 'publish_topics' => true, 'edit_topics' => true, 'edit_others_topics' => false, 'delete_topics' => false, 'delete_others_topics' => false, 'read_private_topics' => true, // Reply caps 'publish_replies' => true, 'edit_replies' => true, 'edit_others_replies' => false, 'delete_replies' => false, 'delete_others_replies' => false, 'read_private_replies' => true, // Topic tag caps 'manage_topic_tags' => false, 'edit_topic_tags' => false, 'delete_topic_tags' => false, 'assign_topic_tags' => true, ); break; default : return $role; } }
这样加入的用户角色,会在后台--用户--编辑用户 这个界面产生一个 额外权限(additional capabilitis),如果以后用不上这个特殊角色了,这个额外权限是删除不掉的,为了解决和个问题,我们需要用到另外一段代码和一个插件user role editor。
如果需要删除上面这个角色,那么我们就需要将下面这段代码放在functions.php里,再到后台开启user role editor这个插件,进入用户菜单,找到刚刚变更过角色的用户,点击编辑,我们看到有一个“动作”选项,点击后面的链接,进入user role editor 编辑器,取消不需要的用户角色之前的勾选,保存即可。
最后,记得要到functions.php里删除这段代码。
代码如下:
function sefsefawddad() { $bbp_roles = new BBP_Roles(); $bbp_roles->remove_role("bbp_senior_participate"); } add_action('shutdown', 'sefsefawddad');