Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add/Change User roles

Viewing 11 replies - 1 through 11 (of 11 total)
  • @robkk

    Moderator

    Create new roles with custom capabilities.

    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 author bbp_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 use strtolower() 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;
    }

    @sbskamey

    Participant

    this is great. thanks Robkk. Just one question… Do I put this into the bbpress.php file in the bbpress folder?

    @sbskamey

    Participant

    I added it here: wp-content/plugins/bbpress/templates/default/bbpress

    Just checking if that is correct?

    @sbskamey

    Participant

    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

    @robkk

    Moderator

    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 Member

    You 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.

    @sbskamey

    Participant

    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!

    @sbskamey

    Participant

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

    @robkk

    Moderator

    @sbskamey

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

    @sbskamey

    Participant

    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,
    Kam

    @robkk

    Moderator

    it would work like this though

    #bbpress-forums .community .bbp-author-role {
      background-color: blue;
    }

    or

    #bbpress-forums .team .bbp-author-role {
      background-color: yellow;
    }

    @sbskamey

    Participant

    it worked! thank you so much! for all your help!!!!!

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