Add/Change User roles
-
Hi support,
http://sincebeingsingle.com/insight/topic/share-site-others/
I want to know how to add a new BBpress user role name.
I would like three roles:
– Team Member
– Community Member
– AmbassadorI would also like each of these roles to have a different colour label.
Just like this website: https://ultimatemember.com/forums/topic/how-did-you-find-out-about-ultimate-member/
I have seen a few topics about this, but these topics are from 1-3 years ago. I want to know if there is easier way to do this than changes .php files?
I have tried User Role Editor and Members plugin with no luck. These plugins add new WordPress user roles, but not BBpress Forum user roles.
I would really like to know how to do this.
Thanks again,
Kam
-
Create new roles with custom capabilities.
TO have individual colors for each role, there is a function that might help. I think you need to create a varialbe to pass the
bbp_get_user_display_role()
with the id of the user in the reply authorbbp_get_reply_author_id()
to get the specific user role of the reply author then pass the user role name into $classes. You might also need to usestrtolower()
to make the class in your code lowercase.function my_reply_class($classes) { $classes[] = 'test-class'; return $classes; } add_filter( 'bbp_get_reply_class','my_reply_class' );
After all that, all you would need to do is add this CSS.
.myrole .bbp-author-role { background-color: blue; }
this is great. thanks Robkk. Just one question… Do I put this into the bbpress.php file in the bbpress folder?
I added it here: wp-content/plugins/bbpress/templates/default/bbpress
Just checking if that is correct?
Okay so I put the above code in here and it seems to work:
plugins > bbpress > includes > users
Again, just checking if that is correct?
Now to change the background colours, god that’s gonna take me another 2 days! :p
this is great. thanks Robkk. Just one question… Do I put this into the bbpress.php file in the bbpress folder?
No. Also the bbpress.php file should be in the root of your theme.
I added it here: wp-content/plugins/bbpress/templates/default/bbpress
Just checking if that is correct?
Don’t mess with any core files.
Okay so I put the above code in here and it seems to work:
plugins > bbpress > includes > users
Again, just checking if that is correct?
Don’t mess with any core files.
Here, I forgot I did this a while back. Add this to your functions.php file in your child theme.
function rk_show_role_classes($classes) { $replyid = bbp_get_reply_author_id(); $bbp_get_role = bbp_get_user_display_role($replyid); $bbp_display_role = strtolower($bbp_get_role); $classes[] = $bbp_display_role; return $classes; } add_filter( 'bbp_get_topic_class','rk_show_role_classes' ); add_filter( 'bbp_get_reply_class','rk_show_role_classes' );
Do the CSS I said before for the above code.
It would be something like this but for each role.
.ambassador .bbp-author-role { background-color: blue; }
And if you do not need custom roles but instead just role names, lets say for each role it would be like this.
Keymaster -> Ambassador
Moderator -> Team Member
Participant -> Community MemberYou can just change the role names using this function. Add it to your child themes functions.php file. And rename My Custom Role to what you want.
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' => 'My Custom Keymaster Role Name', 'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() ) ), // Moderator bbp_get_moderator_role() => array( 'name' => 'My Custom Moderator Role Name', 'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() ) ), // Participant bbp_get_participant_role() => array( 'name' => 'My Custom Participant Role Name', 'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) ), // Spectator bbp_get_spectator_role() => array( 'name' => 'My Custom Spectator Role Name', 'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() ) ), // Blocked bbp_get_blocked_role() => array( 'name' => 'My Custom Blocked Role Name', 'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() ) ) ); }
Oh and quit trying to copy Ultimate Members site, try to have a unique website.
Thanks for this Robkk – I’ll give it a go.
(Oh and I’m going to re-design the whole look and feel (that even goes for the role names, going to change them :p), I just want to get it all set up. I’m a designer by trade (hence the dev questions), but I find I get lost in the design when the functionality is not there, so just trying to get it all setup first, that way i can design/css with ease) 🙂
thanks again, will give it a go!
Hi Robkk, I’d like to change the capabilities for the roles, so I think adding the new roles would be better.
When I move my code to my Child Theme, my website goes blank.
Do you know if theres something that I’m missing?
This is my Child Theme with the 3 new role and new capabilities in it… (when I add this code to the core bbpress files, it works, but now that I moved it to my Child Theme, my site goes blank).
<?php // enqueue the child theme stylesheet Function wp_schools_enqueue_scripts() { wp_register_style( 'childstyle', get_stylesheet_directory_uri() . '/style.css' ); wp_enqueue_style( 'childstyle' ); } add_action( 'wp_enqueue_scripts', 'wp_schools_enqueue_scripts', 11); //code to add roles function add_new_roles( $bbp_roles ) { /* Add a role called team member */ $bbp_roles['bbp_teammember'] = array( 'name' => 'Team Member', 'capabilities' => custom_capabilities( 'bbp_teammember' ) ); /* Add a role called teammember */ $bbp_roles['bbp_communitymember'] = array( 'name' => 'Community Member', 'capabilities' => custom_capabilities( 'bbp_communitymember' ) ); /* Add a role called ambassador */ $bbp_roles['bbp_ambassador'] = array( 'name' => 'Ambassador', 'capabilities' => custom_capabilities( 'bbp_ambassador' ) ); 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_teammember' ) $caps = custom_capabilities( $role ); if( $role == 'bbp_communitymember' ) $caps = custom_capabilities( $role ); if( $role == 'bbp_ambassador' ) $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 'teammember' role */ case 'bbp_teammember': return array( // Primary caps 'spectate' => true, 'participate' => true, 'moderate' => true, 'throttle' => true, 'view_trash' => true, // Forum caps 'publish_forums' => true, 'edit_forums' => true, 'edit_others_forums' => true, 'delete_forums' => true, 'delete_others_forums' => true, '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, ); /* Capabilities for 'communitymember' role */ case 'bbp_communitymember': 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' => false, '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' => true, ); /* Capabilities for 'ambassador' role */ case 'bbp_ambassador': 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' => false, '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' => true, ); break; default : return $role; }
at the very end of this function
function custom_capabilities( $role )
You see this
); break; default : return $role; }
Well your missing a
}
at the very end.the end of the function should look like this.
); break; default : return $role; } }
Ahh, that’s brilliant. It works! Thanks so much! You’re a god!
My last question is about changing the background colours. I’m using your code:
function rk_show_role_classes($classes) { $replyid = bbp_get_reply_author_id(); $bbp_get_role = bbp_get_user_display_role($replyid); $bbp_display_role = strtolower($bbp_get_role); $classes[] = $bbp_display_role; return $classes; } add_filter( 'bbp_get_topic_class','rk_show_role_classes' ); add_filter( 'bbp_get_reply_class','rk_show_role_classes' );
and added in some css, like you said:
.communitymember .bbp-author-role { background-color: blue; }
This may seem like a basic question, but am I suppose to amend your code in any way? If so, I have tried multiple variations of it (logic guess work), and a few variations of css, with no luck.
Please could you guide me as to what I need to change on your code to get it to work?
Thanks again,
Kamit would work like this though
#bbpress-forums .community .bbp-author-role { background-color: blue; }
or
#bbpress-forums .team .bbp-author-role { background-color: yellow; }
it worked! thank you so much! for all your help!!!!!
- You must be logged in to reply to this topic.