Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add Custom User Roles


  • Alice Kaye
    Participant

    @alice-kaye

    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 25 replies - 1 through 25 (of 31 total)

  • Alice Kaye
    Participant

    @alice-kaye

    Update – I managed to get it to work and managed to change the name of the defaults, to make things easier.

    This is the code in case anyone needs it for reference in the future:

    /* 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
    );
    return $bbp_roles;
    }
    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );
    /* bbPress Custom Roles */
    
    /* bbPress Custom Role Names */
    add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );
    
    function ntwb_bbpress_custom_role_names() {
    	return array(
    
    		// Keymaster - Advisor
    		bbp_get_keymaster_role() => array(
    			'name'         => 'Advisor',
    			'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
    		),
    
    		// Moderator - Moderator
    		bbp_get_moderator_role() => array(
    			'name'         => 'Councilman',
    			'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
    		),
    
    		// Participant - Apprentice
    		bbp_get_participant_role() => array(
    			'name'         => 'Apprentice',
    			'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
    		),
    	);
    }
    /* bbPress Custom Role Names */

    Alice Kaye
    Participant

    @alice-kaye

    Secondary Update:

    After making aforementioned changes, while I was able to change the name of the three major roles (Keymaster > Advisor, Moderator > Councilman, Participant > Apprentice), I’ve found that the newly added roles are no longer operational (nor showing).

    Screenshot: http://screencast.com/t/p28WJahSY4NU

    Can someone please take a look at my code and maybe provide some insight as to where I have gone wrong?

    Thanks in advance!

    Edit: I have tried modifying the newly added roles to the following:

    From:

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

    To:

    $bbp_roles['bbp_craftsman'] = array(
    'name' => 'Craftsman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_apprentice_role() ) // the same capabilities as participants
    );

    Which gave me immediate errors after re-uploading the function.php. If I leave it as is, I do not get any errors, they just simply don’t show.


    Robin W
    Moderator

    @robin-w

    there are two filters in the second example, and the second one wipes out the first !

    I tried your original code and it works fine, suggest you post it back and try it again, you may just have missed a character in the copy/paste.


    Alice Kaye
    Participant

    @alice-kaye

    Hi Robin,

    Thanks for replying and pointing out the issue for me. Woot! I see the two filters, in post 2. I’ll try to merge the two filters together, remove the second filter entirely and hopefully with the merge, it will work the way I’m intending. (This is literally the first time I’ve ever tried my hand at understanding PHP, so I’m totally new to this).

    The first bit of code (post 1) is simply just making new roles entirely.

    Post 2 is what I need to have. I want to change the name of the main three roles, and then add the others. Reason being, for starters, I think it will confuse my users to have those three the we won’t use, and I can’t seem to change my admit account from Keymaster to one I create.

    Thanks so much! ๐Ÿ™‚


    Alice Kaye
    Participant

    @alice-kaye

    Okay, so I tried to modify the function, to merge the two and that didn’t work, so I have a few questions:

    From this:

    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );
    
    add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );

    To this:
    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 'ntwb_bbpress_custom_role_names' 1 );

    But that didn’t work at all, wound up immediately getting an error.

    Should I perhaps be placing the renaming of the base three roles, before the creation of the new four roles? The filter for the creation of the roles comes after all of them are created but the filter for the role name change comes before the changes, so I wonder if the two filters would still conflict if I moved them around?

    These are really newbie questions, I know, but I am not 100% sure as to how to change these to work in tandem.

    Thanks in advance for any help you can provide!


    Robin W
    Moderator

    @robin-w

    ok, quick answers and I’ll come back in a short while

    a filter is a way to alter core code without having to change the core files. In effect if a function on the core is called ‘fred’ and writes the word ‘fred’ to your screen, then (as long as the function author has left the right code in there) you can write a filter to change ‘fred’ to ‘bert’ by adding a function (say a function called Bert) that writes ‘bert’ and then ‘add filter(‘fred’,’bert’) tells the system to ignore the fred function and put in the bert function, so it writes bert.

    Now you have two filters which are overwriting what the core code in ‘bbp_get_dynamic_roles’ does, so in your original code you only had one filter so was fine.

    In your second version, the first filter did the new roles, but the second filter to the same code overwrites the first (thus nullifying it) , so you were just left with the second one working.

    bit like

    original says write Fred
    First filter says write Bert
    Second filter says write Harry

    So the code writes only Harry

    BUT simply combining the two won’t work either as a filter has to return something and only one thing, so the code will quite as soon as it encounters a ‘return’ command.

    Since the first half has ‘return $bbp_roles; ‘ it quits at that point and the second half isn’t exectuted.

    So we need one function with one return and one filter (you actually can have more than one filter, but that’s more advanced!) – I’ll now go and look at it

    see also

    Step by step guide to setting up a bbPress forum – part 5


    Robin W
    Moderator

    @robin-w

    ok, so the following combines what the first does with what the second does and achieves what you want

    /* bbPress Custom Roles */
    function add_custom_role( $bbp_roles ) {
     
    
    $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
    );
    $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 keymaster
    );
    $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 );

    Alice Kaye
    Participant

    @alice-kaye

    Hi Robin,

    Thank you for that link. It does answer some questions, but presents many more, probably since I don’t have any foundation (personally) in PHP. This is the first time I’ve ever tried to write/modify something in PHP and actually have it work.

    I understand having one filter and one function and that they’re essentially both trying to grab the attention of bbp_get_dynamic_roles if both are present. I also now understand that by trying to merge them, that’s impossible because it needs to be 1 v 1, not 1 v 2. But if I can only have 1 function and 1 filter, I’m then left wondering if this can be written in some way to encompass both things that I’m trying to accomplish.


    Alice Kaye
    Participant

    @alice-kaye

    Oh wow, you got that written up fast! I re-wrote my response to you about 10 times while you were modifying that. I’m going to look through and see what you changed, so that I can learn the differences between the two pieces of code.

    Thank you so much for modifying it! I’ll add it now and see how it pans out.


    Alice Kaye
    Participant

    @alice-kaye

    So basically, am I to understand correctly that you simply removed:

    /* bbPress Custom Role Names */
    add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );
    
    function ntwb_bbpress_custom_role_names() {
    	return array(

    Which then left you with one function and one filter, but due to the way the code is written for the base three roles (keymaster, moderator and participants), that code is changing their name, regardless of the function and filter?

    Hope I’m grasping that correctly.


    Robin W
    Moderator

    @robin-w

    well worth getting into php if you plan to do customisation, and yes have a look and see if you can work out what I did.

    The key is that a function can only have one ‘return’ command. The WHOLE of the second functioj is one long return command (it starts with it). so I just amended the first one from of creating the final three roles, to calling up the existing roles and then overwriting them

    Have fun, and come back with any queries – it was only two years ago that I wrote my first line of php code, and I only do this very part time!


    Alice Kaye
    Participant

    @alice-kaye

    It’s really great to know that you’ve learned this much in two years. I have hope for me yet of understanding this!

    What I see that you did was move return $bbp_roles; from the middle of the code, down to the bottom, while also removing the secondary and unnecessary function and filter, keeping only the 'add_custom_role' portion of the function/filter.

    I am definitely curious as to how it’s working without the 'ntwb_bbpress_custom_role_names' filter, for the renaming of the roles? As I write this, I’m not even sure how to explain the question I have. Haha.

    But I thank you for all of your help. You’ve been absolutely fantastic and I appreciate all of the information.

    This was actually a ton of fun, despite my code not working out without your help. As someone who has a deep interest in web design (I do it for a living, without the code-end of things), I think this has fueled me into wanting to learn more about how this works and delve deeper! ๐Ÿ˜€


    Robin W
    Moderator

    @robin-w

    Hope Iโ€™m grasping that correctly.

    nearly

    I scrapped part 2, then

    what I actually did was take the final three roles form your original part 1 (I moved the participant one down to make it consistent)

    so if you look at the councilman role

    Then where you had

    $bbp_roles['my_custom_role6'] = array(
    'name' => 'Councilman',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as keymaster
    );
    
    

    This will set a role called my-custom-role-6 to the name and capabilities, and if that role doesn’t exist will create it. WordPress generally doesn’t worry about whether something already exists, if it does it overwrites it, and if it doesn’t it creates it.

    Now I could have simply changed ‘my_custom_roles_6 to bbp_keymaster, and it would have changed the existing role bbp_keymaster name and capabilities to what was in that line

    But for reasons I won’t bore you with, it is better to call a function which gets that role

    hence the new line before

    $keymaster = bbp_get_keymaster_role() ;
    
    

    Then you just use that $keymaster variable set in the line before


    Alice Kaye
    Participant

    @alice-kaye

    So, follow-up question to that then…

    Would it have been wrong to modify them to say bbp_craftsman, bbp_journeyman, rather than my-custom-role-1, my-custom-role-2? Does this have some bearing on the code, aside from a name, that I’m not understanding?

    I saw that you had used the original code with my-custom-role-1/2/3/etc, and modified them back to the bbp_name, thinking it was simply just a name. If it’s more than that, I’d love to know.


    Robin W
    Moderator

    @robin-w

    Would it have been wrong to modify them to say bbp_craftsman

    either is fine, they’re just labels under which they are stored in the database, so yes you could, but as you’ll not generally see these it doesn’t matter either way, but if it’s clearer to you, the go ahead and amend !

    Keep asking !


    Alice Kaye
    Participant

    @alice-kaye

    Oh great! I figured they were just labels but I wasn’t sure. I’m incredibly OCD so I try to mark everything, to make it easier for me in the long term.

    I’m sure I’ll have more thoughts on this whole thing as I go. I’m just so happy to have user roles that are actually functioning the way I want them to. Woot!

    Thanks for all of your help. I’ve truly appreciated every moment of it. ๐Ÿ™‚


    Robin W
    Moderator

    @robin-w

    don’t forget you can use

    /* xxx */ or
    //

    to make comments throughout your code

    // only does a single line

    /* this
    does
    multiple lines
    */

    so you can have

    //Add the Artisan role
    $bbp_roles['my_custom_role5'] = array(
    'name' => 'Artisan',
    'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) // the same capabilities as keymaster
    );
    // Rename the Moderator role
    $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 keymaster
    );
    );
    
    

    Alice Kaye
    Participant

    @alice-kaye

    You know, I kept seeing that throughout the code and thought that was pretty awesome. I will definitely make use of that! It’s a lot easier and cleaner looking and 9 times out of 10, I don’t need two lines.

    Thank you for the tip! ๐Ÿ˜€


    Robin W
    Moderator

    @robin-w

    it’s also great when you come back to the code two months later and can’t remember what it does !


    Alice Kaye
    Participant

    @alice-kaye

    Yeah, I’ve been using /**/ for all of my commenting, but I really like the // style for one line comments, so I’m going to move everything over to that format.

    I try to label everything both for OCD reasons and simply for the exact reason you mentioned. I don’t want to find myself wondering in the future what the heck the code was placed for! ๐Ÿ˜›


    Alice Kaye
    Participant

    @alice-kaye

    Hey @robin-w,

    Along the same vein as this, I just signed up one of my friends as a user to test privileges and make sure it’s all working as it should be. When I signed him up, he was not automatically assigned a forum role. Is that due to the changes I’ve made or is this normal?

    I want to make sure that I didn’t make a mistake somewhere in all of this. I’d like the base user role to be “Apprentice” so that when they sign up on the site, they have the lowest role.

    If this is something that has to be set for each user, that isn’t a problem, again, I just want to make sure I didn’t muck it up.

    Thanks! ๐Ÿ™‚

    Edit: I see that there is an option, of course, in the Settings ยป Forum, it was set to Advisor (since the others didn’t exist earlier). But it didn’t set him as Advisor. It just set him to None for forum role.

    2nd Edit: (Firstly, I know that I’m nuts, I just keep thinking of other things). I installed a plugin called BuddyPress Registration and I’m wondering since that locks down the entire BuddyPress/bbPress system until users are approved, if maybe that’s why it’s set to none initially, so that they have zero access to the forums. -ponders-


    Robin W
    Moderator

    @robin-w

    if you changed roles, then you might need to reset the default role

    dashboard>settings>forums and look for what the default role is


    Alice Kaye
    Participant

    @alice-kaye

    Okay, that was my assumption as well. I’ll be signing up my husband as a user here shortly, so I’ll see what it does when I get him squared away. I’ll post my results here. I try to make sure I always leave a message for anyone who winds up curious about the same topic(s) in the future. ๐Ÿ™‚


    Alice Kaye
    Participant

    @alice-kaye

    Just following up on this. It seems to have worked properly after I set it again to Apprentice and re-saved. ๐Ÿ™‚

    All set here! Woot!


    Robin W
    Moderator

    @robin-w

    ๐Ÿ™‚

Viewing 25 replies - 1 through 25 (of 31 total)
  • You must be logged in to reply to this topic.
Skip to toolbar