Extending BBPress Private Forums
by Antoine KhaterDisclaimer This is based on Aditya Naik ‘s BBPress Private forums plugin version 5.2, I needed to add a custom role to this website forums so I hacked the code. Aditya Naik is the original writer of the code and all credits goes to him.
When to use this hack: If, like me, you need to add some private forums in your BBPress setup but you don’t want to make those users moderators or administrators then here is how to add a custom role.
Difficulty: Easy Estimated time: 10~15 min Files to edit: 2
- Start by downloading and installing BBPress private forums plugin
- Decide on the new role name e.g. in my case Translator
- In your BBPress directory open the “bb-includes” directory and backup capabilities.php
- Open capabilities.php and go to line 135 (BBPress 0.8.3) you should have something like
'member' => array(
'name' => __('Member'),
'capabilities' => array(
'participate' => true,
'edit_favorites' => true,
'edit_tags' => true,
'edit_topics' => true,
'edit_posts' => true,
'edit_profile' => true,
'write_topics' => true,
'write_posts' => true,
'change_password' => true,
'read' => true
)),
This is the definition of a member for BBPress.
- Copy these lines and paste them directly after this section then replace the word ‘member’ in the first 2 lines by the desired new role name i.e. Translator in my case. Your code should now look like
'translator' => array(
'name' => __('Translator'),
'capabilities' => array(
'participate' => true,
'edit_favorites' => true,
'edit_tags' => true,
'edit_topics' => true,
'edit_posts' => true,
'edit_profile' => true,
'write_topics' => true,
'write_posts' => true,
'change_password' => true,
'read' => true
)),
'member' => array(
'name' => __('Member'),
'capabilities' => array(
'participate' => true,
'edit_favorites' => true,
'edit_tags' => true,
'edit_topics' => true,
'edit_posts' => true,
'edit_profile' => true,
'write_topics' => true,
'write_posts' => true,
'change_password' => true,
'read' => true
)),
Wonderful ! what we did so far is crating a new role “translator” that is exactly the same as “member”. Let’s now define a new privilege, the new privilege I need for this role is to be able to read the “Translator’s forum” so I defined it as
'read_translators_forums' => true
- Add this new created privileged to EVERY role already defined in capabilities.php that should read that forum. I added this privilege to all roles but members. For instance my ‘translators’ role now looks like that
'translator' => array(
'name' => __('Translator'),
'capabilities' => array(
'participate' => true,
'edit_favorites' => true,
'edit_tags' => true,
'edit_topics' => true,
'edit_posts' => true,
'edit_profile' => true,
'write_topics' => true,
'write_posts' => true,
'change_password' => true,
'read_translators_forums' => true,
'read' => true
)),
Notice the coma “,” after the priviledge don’t forget to add it.
- Good now save your capabilities.php
- Browse to BBpress\my-plugins\private-forums and backup private-forums.php
- Open private-forums.php
- Locate
function private_forums_display_role_dropdown($name, $index, $role)
and add to the select list your new created role, in my case
<option value="TRANSLATOR" <?php echo ($role == 'TRANSLATOR') ? 'selected' : '' ; ?>>Translators</option>
My Select list looks like
<select name="<?php echo $name . '[' . $index . ']'; ?>" id="<?php echo $name . '_' . $index ; ?>"> <option ($role="=" value="OPEN">>Open to all</option> <option ($role="=" value="MEMBER">>Registered Members</option> <option ($role="=" value="TRANSLATOR">>Translators</option> <option ($role="=" value="MODERATOR">>Moderators</option> <option ($role="=" value="ADMINISTRATOR">>Administrators</option> </select>
Ok we’re almost there
- Now locate
function private_forums_check_user_access_to_forum($access) {
and add a case for your newly created role with the newly created privilege something like
case 'TRANSLATOR':
return bb_current_user_can('read_translators_forums');
break;
My function is
function private_forums_check_user_access_to_forum($access) {
switch($access) {
case 'MODERATOR':
return bb_current_user_can('moderate');
break;
case 'ADMINISTRATOR':
return bb_current_user_can('administrate');
break;
case 'MEMBER':
return bb_current_user_can('participate');
break;
case 'TRANSLATOR':
return bb_current_user_can('read_translators_forums');
break;
case 'OPEN':
return true;
break;
default:
return true;
}
All you still have to do now is activate the plugin and enjoy it.
N.B.: If you feel uneasy about editing capabilities.php there is another (better?) way to add a role using a bbpress plugin.
May 29th, 2010 at 18:33
Fantastic – thank you very much for going to the trouble of documenting this. I am going to implement this right now as I don’t have too much know how on PHP and this is exactly what I need to do with a private forum which will be exclusive to one role. Thanks a stack!