Custom CapabilitiesCustom Capabilities
Codex Home → Custom Capabilities
Whilst bbPress provides a good set of forum roles, with distinct capabilities, you may want to amend these or add new names.
However the latest combination of WordPress and bbPress does not work if the code to do this is in the functions.php file of the child theme (or indeed parent theme!).
If you put this code in a plugin, it seems to work fine.
If you are comfortable in changing files and FTPing them to your site, then visit bbpress role adder – Robin W』s website (rewweb.co.uk) for a plugin and template that should work
Adding new names
At the simplest levels, you can add a new role name and give it existing capabilities
This can be useful just for your own admin purposes, but also if like me you hate the 「keymaster」 role name, just create a new role and give it keymaster capabilities.
The following code creates three new roles with names 『name 1′,』name 2′ and 『name 3′ the first role has participant, the second moderator and the third keymaster.
Simply amend the code to create as many roles with whatever capabilities you need
add_action (『wp_loaded』 , 『load_new_roles』) ;
function load_new_roles ()
{
add_filter( 『bbp_get_dynamic_roles』, 『add_custom_role』, 1 );
}
function add_custom_role( $bbp_roles )
{
$bbp_roles[『my_custom_role1』] = array(
『name』 =>』name 1′,
『capabilities』 =>bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles[『my_custom_role2』] = array(
『name』 =>』name 2′,
『capabilities』 =>bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles[『my_custom_role3』] = array(
『name』 =>』name 3′,
『capabilities』 =>bbp_get_caps_for_role( bbp_get_keymaster_role() ) // the same capabilities as keymaster
);
return $bbp_roles;
}
Creating new roles
If you want to add a new roles with specific capabilities, then you can add these
The code below adds a role called 『tutor』 simply change the word tutor wherever it occurs to the name you want and decide what capabilities you want to the role to have.
//code to add tutor role
add_action ('wp_loaded' , 'load_new_roles') ;
function load_new_roles ()
{
add_filter( 'bbp_get_dynamic_roles', 'add_new_roles', 1 );
add_filter( 'bbp_get_caps_for_role', 'add_role_caps_filter', 10, 2 );
}
function add_new_roles( $bbp_roles )
{
/* Add a role called tutor */
$bbp_roles['bbp_tutor'] = array(
'name' =>'Tutor',
'capabilities' =>custom_capabilities( 'bbp_tutor' )
);
return $bbp_roles;
}
function add_role_caps_filter( $caps, $role )
{
/* Only filter for roles we are interested in! */
if( $role == 'bbp_tutor' )
$caps = custom_capabilities( $role );
return $caps;
}
function custom_capabilities( $role )
{
switch ( $role )
{
/* Capabilities for 'tutor' role */
case 'bbp_tutor':
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;
}
}
The code below adds two roles, tutor and pupil. Using this you should be able to add any number of roles
add_action ('wp_loaded' , 'load_new_roles') ;
function load_new_roles () {
add_filter( 'bbp_get_dynamic_roles', 'add_new_roles', 1 );
add_filter( 'bbp_get_caps_for_role', 'add_role_caps_filter', 10, 2 );
}
function add_new_roles( $bbp_roles )
{
/* Add a role called tutor */
$bbp_roles['bbp_tutor'] = array(
'name' =>'Tutor',
'capabilities' =>custom_capabilities( 'bbp_tutor' )
);
/* Add a role called pupil */
$bbp_roles['bbp_pupil'] = array(
'name' =>'Pupil',
'capabilities' =>custom_capabilities( 'bbp_pupil' )
);
return $bbp_roles;
}
function add_role_caps_filter( $caps, $role )
{
/* Only filter for roles we are interested in! */
if( $role == 'bbp_tutor' )
$caps = custom_capabilities( $role );
if( $role == 'bbp_pupil' )
$caps = custom_capabilities( $role );
return $caps;
}
function custom_capabilities( $role )
{
switch ( $role )
{
/* Capabilities for 'tutor' role */
case 'bbp_tutor':
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,
);
/* Capabilities for 'pupil' role */
case 'bbp_pupil':
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;
}
}