BBpress Custom Roles/Rename Existing Roles

Hello,

I’m trying to rename the current roles as well as ADD new roles. When I add the filter to change the current role name, it doesn’t add the new user role I’ve added. If I remove that code, then the new user role shows up.

This is the code I’m using. The code for adding new user came directly from BBpress codex:

https://codex.bbpress.org/custom-capabilities/

//BBpress Custom Roles //
function add_new_roles( $bbp_roles )
{
/* Add a role called tutor */
$bbp_roles['bbp_lead'] = array(
'name' => 'Community Lead',
'capabilities' => custom_capabilities( 'bbp_lead' )
);

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_lead' )
$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 'tutor' role */
case 'bbp_lead':
return array(
// Primary caps
'spectate' => true,
'participate' => true,
'moderate' => true,
'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' => true,

// Topic caps
'publish_topics' => true,
'edit_topics' => true,
'edit_others_topics' => true,
'delete_topics' => true,
'delete_others_topics' => true,
'read_private_topics' => true,

// Reply caps
'publish_replies' => true,
'edit_replies' => true,
'edit_others_replies' => true,
'delete_replies' => true,
'delete_others_replies' => true,
'read_private_replies' => true,

// Topic tag caps
'manage_topic_tags' => true,
'edit_topic_tags' => true,
'delete_topic_tags' => true,
'assign_topic_tags' => true,
);

break;

default :
return $role;
}
}
// End BBpress Custom Roles //

//BBpress Rename Roles //
/* BBPress Renaming Roles */
add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );
function ntwb_bbpress_custom_role_names() {
return array(
// Keymaster
bbp_get_keymaster_role() => array(
'name' => 'Administrator',
'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
),
// Moderator
bbp_get_moderator_role() => array(
'name' => 'Moderator',
'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
),
// Participant
bbp_get_participant_role() => array(
'name' => 'Member',
'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
),
// Spectator
bbp_get_spectator_role() => array(
'name' => 'Spectator',
'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
),
// Blocked
bbp_get_blocked_role() => array(
'name' => 'Blocked',
'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
));}
//BBpress Rename Roles End //

  • Ash
    • WordPress Hacker

    Hi Joshua

    Coloring role like your screenshot would not be easy I think, if you want I can move your thread to members’ forum so that other members can chime in to help.

    About the reordering, it should show how you set the order in ntwb_bbpress_custom_role_names() function. And as per your screenshot, I can see it follow the order.

    Or do you want like this – Administrator, Community Lead, Moderator, and so on…. means Community Lead is just after administrator?

  • Ash
    • WordPress Hacker

    In that case, you can remove the following:

    function add_new_roles( $bbp_roles )
    {
    /* Add a role called tutor */
    $bbp_roles['bbp_lead'] = array(
    'name' => 'Community Lead',
    'capabilities' => custom_capabilities( 'bbp_lead' )
    );

    return $bbp_roles;
    }

    add_filter( 'bbp_get_dynamic_roles', 'add_new_roles', 999 );

    and then update the following:

    add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );
    function ntwb_bbpress_custom_role_names() {
    return array(
    // Keymaster
    bbp_get_keymaster_role() => array(
    'name' => 'Administrator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
    ),
    'bbp_lead' => array(
    'name' => 'Community Lead',
    'capabilities' => custom_capabilities( 'bbp_lead' )
    ),
    // Moderator
    bbp_get_moderator_role() => array(
    'name' => 'Moderator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
    ),
    // Participant
    bbp_get_participant_role() => array(
    'name' => 'Member',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
    ),
    // Spectator
    bbp_get_spectator_role() => array(
    'name' => 'Spectator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
    ),
    // Blocked
    bbp_get_blocked_role() => array(
    'name' => 'Blocked',
    'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
    )
    );
    }

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,

    Ash

  • Joshua
    • The Incredible Code Injector

    Hi Ash ,

    This code works for registering the new role. I can see it, and I can select it for a user. However; I’m a bit confused as to if it’s actually working. I created a new role called “Community Lead”. The initial code I select should not give that role moderation capabilities, however; when I log in with that user they have full moderation capabilities. Even when I’ve set everything to “false” it still has moderation privileges.

  • Joshua
    • The Incredible Code Injector

    Hi Ash ,

    Here is the code:

    //BBpress Register New Role//

    function add_role_caps_filter( $caps, $role )
    {
    /* Only filter for roles we are interested in! */
    if( $role == 'bbp_community_lead' )
    $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 'tutor' role */
    case 'bbp_community_lead':
    return array(
    // Primary caps
    'spectate' => false,
    'participate' => false,
    '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' => false,
    'edit_others_topics' => false,
    'delete_topics' => false,
    'delete_others_topics' => false,
    'read_private_topics' => false,

    // Reply caps
    'publish_replies' => true,
    'edit_replies' => false,
    'edit_others_replies' => false,
    'delete_replies' => false,
    'delete_others_replies' => false,
    'read_private_replies' => false,

    // Topic tag caps
    'manage_topic_tags' => false,
    'edit_topic_tags' => false,
    'delete_topic_tags' => false,
    'assign_topic_tags' => false,
    );

    break;

    default :
    return $role;
    }
    }

    //BBpress Rename Roles //
    add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );
    function ntwb_bbpress_custom_role_names() {
    return array(
    // Keymaster
    bbp_get_keymaster_role() => array(
    'name' => 'Administrator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
    ),
    'bbp_community_lead' => array(
    'name' => 'Community Lead',
    'capabilities' => custom_capabilities( 'bbp_community_lead' )
    ),
    // Moderator
    bbp_get_moderator_role() => array(
    'name' => 'Moderator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
    ),
    // Participant
    bbp_get_participant_role() => array(
    'name' => 'Member',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
    ),
    // Spectator
    bbp_get_spectator_role() => array(
    'name' => 'Spectator',
    'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
    ),
    // Blocked
    bbp_get_blocked_role() => array(
    'name' => 'Blocked',
    'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
    )
    );
    }
    // End BBpress Custom Roles //

  • Ash
    • WordPress Hacker

    The code looks okay to me. But in wordpress, roles and capabilities work in a different way. It saves in database and if you don’t delete, new code doesn’t work.

    I recommend to test changing the role name. Keep the same code, but change the name and slug of the role. What I mean is to change bbp_community_lead and Community Lead to something else. Then create a new user with the new role and check.

    Let us know how it goes.

    Have a nice day!

    Cheers,

    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.