Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add Custom User Roles

  • @alice-kaye

    Participant

    Hi everyone,

    I’m working on creating some custom user roles and I was wondering if I could get assistance with it, because the code I modified kind of killed my site (so I took it out and it’s working fine again).

    I’m running a guild website, so we have specific titles (in order from left to right for hierarchy):
    Advisor (Keymaster) > Councilman (Moderator) > Artisan (Moderator) > Adept (Participant) > Journeyman (Participant) > Craftsman (Participant) > Apprentice (Participant)

    I was trying to follow this guide: http://codex.bbpress.org/custom-capabilities/

    /* bbPress Custom Roles */
    function add_custom_role( $bbp_roles ) {
     
    $bbp_roles['my_custom_role1'] = array(
    'name' => 'Apprentice',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['my_custom_role2'] = array(
    'name' => 'Craftsman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['my_custom_role3'] = array(
    'name' => 'Journeyman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['my_custom_role4'] = array(
    'name' => 'Adept',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['my_custom_role5'] = array(
    'name' => 'Artisan',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as keymaster
    );
    $bbp_roles['my_custom_role6'] = array(
    'name' => 'Councilman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as keymaster
    );
    $bbp_roles['my_custom_role7'] = array(
    'name' => 'Advisor',
    'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() ) // the same capabilities as keymaster
    );
    return $bbp_roles;
    }
    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );
    /* bbPress Custom Roles */

    Can someone tell me where I might have gone wrong?

    Thanks in advance! 😀

Viewing 14 replies - 26 through 39 (of 39 total)
  • @alice-kaye

    Participant

    Okay, so kind of resurrecting my old thread here, but with good reason, because something has gone wrong with the code. Today, I promoted someone (the first person to be promoted to this rank) Artisan, which I’ve hand coded into my functions.php file.

    The code:

    /* bbPress Custom Roles */
    function add_custom_role( $bbp_roles ) {
    
    $bbp_roles['bbp_craftsman'] = array(
    'name' => 'Craftsman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['bbp_journeyman'] = array(
    'name' => 'Journeyman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['bbp_adept'] = array(
    'name' => 'Adept',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    $bbp_roles['bbp_artisan'] = array(
    'name' => 'Artisan',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as moderator
    );
    $moderator = bbp_get_moderator_role() ;
    $bbp_roles[$moderator] = array(
    'name' => 'Councilman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as moderator
    );
    $keymaster = bbp_get_keymaster_role() ;
    $bbp_roles[$keymaster] = array(
    'name' => 'Advisor',
    'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() ) // the same capabilities as keymaster
    );
    $apprentice = bbp_get_participant_role() ;
    $bbp_roles[$apprentice] = array(
    'name' => 'Apprentice',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    return $bbp_roles;
    }
    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );
    /* bbPress Custom Roles */

    As you can see, the way I’ve written it, I had Artisan and Councilman both as moderator roles. This is really how I need it set up.

    Currently I’ve adjusted Artisan to:

    $bbp_roles['bbp_artisan'] = array(
    'name' => 'Artisan',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );

    Because for whatever reason, when set to moderator, she could not see our forums at all.

    Is there a way to have two moderator ranks, or can there only be one? If so, where have I gone wrong with this? I’m quite confused over it now.

    Thanks in advance, you guys have been so helpful through all of my learning. 🙂

    @robin-w

    Moderator

    you can have multiple roles with the same capabilities, so the original code should be fine.

    So I would check

    1. that the moderator role which you have renamed Councilman works
    2. that if you set up a test user and give them Artisan, that that works (ie is it only her)

    @alice-kaye

    Participant

    Hi @robin-w!

    Thanks for replying back about this. I’m sorry I failed to mention my troubleshooting. It definitely worked when I set up my husband’s account as a Councilman, and I tested every single rank on his account. He had the same results when set to an Artisan.

    At that point, I came here because I can’t see anything wrong with the code.

    Thanks again for the reply! 🙂

    @robin-w

    Moderator

    ok, I’ve just loaded that code to my test site and it all works fine, the artisan role gives moderator access.

    Not sure what has happened, what other changes have there been to your site?

    He had the same results when set to an Artisan

    Presume you mean he could not access, and by the by what do you mean by that? what happens when they try and access the forums, can they not see menu item, or what do they get when they try??

    @alice-kaye

    Participant

    Hi @robin-w,

    Thanks for the response. Sorry for the slow response, I was away for the weekend.

    I’ll try to replace the code again today, but basically what happens is, when they go to the forums it displays a 404 error as if the page doesn’t exist. They can see the top menu, and the rest of the website’s look, but the forums aren’t visible at all.

    @robin-w

    Moderator

    ok, come back when you’ve re-tried

    @angelfire4xx

    Participant

    Not sure if this topic is out of date…?
    I have WP 5.8.1
    Trying to create a custom role based on the help here, but whatever I do it doesn’t work. Feeling a bit lost as I’m not a developer. I’ve read all the posts on this topic that I can find!
    Added this to the end of functions.php
    /* bbPress Custom Roles */
    function add_custom_role( $bbp_roles ) {
    $bbp_roles[‘my_custom_role1’] = array(
    ‘name’ => ‘Naturopath’,
    ‘capabilities’ => bbp_get_caps_for_role( bbp_get_participant_role() )
    );

    I saw this line sometimes was added so I tried appending that too, but it didn’t help.
    add_filter( ‘bbp_get_dynamic_roles’, ‘add_custom_role’, 1 );
    }

    Any advice? Thanks!

    @robin-w

    Moderator
    //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;
    }
    }

    @angelfire4xx

    Participant

    Thanks Robin, I did search and replace tutor > Naturopath and pasted the code at the end of functions.php. I would love to say it worked but…

    Naturopath appeared as a forum role selection in the dropdown for each WP User. However when Naturopath was selected for the user and then saved, WP only displayed Role = “No role for this user”, and Capabilities = bbp_Naturopath.

    When I tried a test post as that user, BBPress described me as a participant.
    So, not there yet, but your help is appreciated 🙂
    Linda

    (PS: I’m looking for the Naturopath role to have the same capabilities as Participant.)

    @robin-w

    Moderator

    ok, I think it was just loading in the wrong place, try

    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 naturopath */
    $bbp_roles['bbp_naturopath'] = array(
    'name' =>'Naturopath',
    'capabilities' =>custom_capabilities( 'bbp_naturopath' )
    );
    
    return $bbp_roles;
    }
    
    function add_role_caps_filter( $caps, $role )
    {
    /* Only filter for roles we are interested in! */
    if( $role == 'bbp_naturopath' )
    $caps = custom_capabilities( $role );
    
    return $caps;
    }
    
    function custom_capabilities( $role )
    {
    switch ( $role )
    {
    
    /* Capabilities for 'naturopath' role */
    case 'bbp_naturopath':
    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;
    }
    }

    Put this in your child theme’s function file –

    ie wp-content/themes/%your-theme-name%/functions.php

    where %your-theme-name% is the name of your theme

    or use

    Code Snippets

    @angelfire4xx

    Participant

    Great tip about code snippets plugin, I didn’t know about that one, thanks!

    Sadly the user is still showing up in bbpress as Participant. Although I was able to save the user with a Naturopath role in WP.

    @robin-w

    Moderator

    sorry, cab you explain precisely where you mean that it is still showing participant ?

    @angelfire4xx

    Participant

    I’m finding that Naturopath is showing as forum role on the user’s wordpress profile, but only for a short while. WordPress changes it back to Participant after it appears to have saved.
    Participant is showing in the bbpress profile and under the username for their topics and comments in bbpress.

    @robin-w

    Moderator

    hmmm… that’s not happening on my test site – let me think

Viewing 14 replies - 26 through 39 (of 39 total)
  • You must be logged in to reply to this topic.
Skip to toolbar