Skip to:
Content
Pages
Categories
Search
Top
Bottom

Need help with custom roles


  • Bob1nz
    Participant

    @bob1nz

    Hi

    This is my third time trying to post here hopefully it sticks this time.

    I am trying to add some custom roles to the bbpress forums and I have followed the Custom Capabilities codex page.

    It doesn’t appear to be working as intended though as I cannot give myself the owner role

    I would like these roles in bbpress to sync with some custom wp roles made with the members plugin.

    sorry for the wall of text below but its an effort to get this topic posted (i had links in the previous topics)

    here is what i have in my child themes functions.php

    // add bbPress Roles********************************************
    
     
    function add_new_roles( $bbp_roles )
    {
        /* Add a role called BlockFusion Owner */
        $bbp_roles['bbp_owner'] = array(
            'name' => 'BlockFusion Owner',
            'capabilities' => custom_capabilities( 'bbp_owner' )
            );
     
        /* Add a role called Co-Owner */
        $bbp_roles['bbp_co_owner'] = array(
            'name' => 'Co-Owner',
            'capabilities' => custom_capabilities( 'bbp_co_owner' )
            );
     
        /* Add a role called Admin */
        $bbp_roles['bbp_admin'] = array(
            'name' => 'Admin',
            'capabilities' => custom_capabilities( 'bbp_admin' )
            );
     
        /* Add a role called Moderator */
        $bbp_roles['bbp_moderator'] = array(
            'name' => 'Moderator',
            'capabilities' => custom_capabilities( 'bbp_moderator' )
            );
     
        /* Add a role called Member */
        $bbp_roles['bbp_member'] = array(
            'name' => 'Member',
            'capabilities' => custom_capabilities( 'bbp_member' )
            );
     
        /* Add a role called Guest */
        $bbp_roles['bbp_guest'] = array(
            'name' => 'Guest',
            'capabilities' => custom_capabilities( 'bbp_guest' )
            );
     
        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_owner' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_co_owner' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_admin' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_moderator' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_member' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_guest' )
            $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 'BlockFusion Owner' role */
            case 'bbp_owner':
                return array(
    
    				// Keymasters only
    				'keep_gate'             => true,
                    
                    // 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 'Co-Owner' role */
            case 'bbp_co_owner':
                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 'Admin' role */
            case 'bbp_admin':
                return array(
                    // Primary caps
                    'spectate'              => true,
                    'participate'           => true,
                    'moderate'              => true,
                    'throttle'              => true,
                    'view_trash'            => true,
     
                    // 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,
                );
     
                /* Capabilities for 'Moderator' role */
            case 'bbp_moderator':
                return array(
                    // Primary caps
                    'spectate'              => true,
                    'participate'           => true,
                    'moderate'              => true,
                    'throttle'              => true,
                    'view_trash'            => true,
     
                    // 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,
                );
     
                /* Capabilities for 'Member' role */
            case 'bbp_member':
                return array(
                    // Primary caps
                    'spectate'              => true,
                    'participate'           => true,
                    'moderate'              => false,
                    'throttle'              => true,
                    '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'         => true,
                    'delete_others_topics'  => false,
                    'read_private_topics'   => true,
     
                    // Reply caps
                    'publish_replies'       => true,
                    'edit_replies'          => true,
                    'edit_others_replies'   => false,
                    'delete_replies'        => true,
                    'delete_others_replies' => false,
                    '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 'Guest' role */
            case 'bbp_guest':
                return array(
                    // Primary caps
                    'spectate'              => true,
                    'participate'           => true,
                    'moderate'              => false,
                    'throttle'              => true,
                    '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'         => true,
                    'delete_others_topics'  => false,
                    'read_private_topics'   => true,
     
                    // Reply caps
                    'publish_replies'       => true,
                    'edit_replies'          => true,
                    'edit_others_replies'   => false,
                    'delete_replies'        => true,
                    'delete_others_replies' => false,
                    '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;
        }
    }
    
Viewing 9 replies - 1 through 9 (of 9 total)

  • Robin W
    Moderator

    @robin-w

    Thanks for posting the code, which if needed I’ll delve into

    but firstly

    It doesn’t appear to be working as intended though as I cannot give myself the owner role
    

    Can you give a bit more detail here as to exactly what you are doing, and where what you expect to happen isn’t, and any error messages etc.

    That is fully explain what ‘I cannot give myself’ means

    Thanks


    Bob1nz
    Participant

    @bob1nz

    Hi 🙂

    Well I am setting up a small community for a minecraft server and we have a plugin for minecraft that will sync the ranks in game to the website http://www.spigotmc.org/resources/communitybridge.2232/

    So I am trying to replicate the ranks in game on the wordpress website.

    The end goal is
    InGameRank -> WordPressRole -> bbPressRole
    Owner -> Owner -> Owner
    Co-Owner -> Co-Owner -> Co-Owner
    Admin -> Admin -> Admin
    Moderator -> Moderator -> Moderator
    Member -> Member -> Member
    Guest -> Guest -> Guest

    The owner would essentially be bbPress’s keymaster but I would like to define the capabilities of each role.

    Using the code I posted I am unable to change my forum role.
    I am currently still set as Administrator in the Site Role.
    When I select my user in the dashboard and select change the forum role to the owner rank I created and click change it just reloads the page and my forum role is still keymaster.
    I am able to change other users forum roles correctly.

    My main question is how to get the wordpress roles to sync to the bbPress roles effectively making it so if i change the wordpress role the bbpress role will also change.

    (what is up with the tag changes I did not put those)


    Bob1nz
    Participant

    @bob1nz

    I found this

    /**
     * Return a map of WordPress roles to bbPress roles. Used to automatically grant
     * appropriate bbPress roles to WordPress users that wouldn't already have a
     * role in the forums. Also guarantees WordPress admins get the Keymaster role.
     *
     * @since bbPress (r4334)
     *
     * @return array Filtered array of WordPress roles to bbPress roles
     */
    function bbp_get_user_role_map() {
    
    	// Get the default role once here
    	$default_role = bbp_get_default_role();
    
    	// Return filtered results, forcing admins to keymasters.
    	return (array) apply_filters( 'bbp_get_user_role_map', array (
    		'administrator' => bbp_get_keymaster_role(),
    		'editor'        => $default_role,
    		'author'        => $default_role,
    		'contributor'   => $default_role,
    		'subscriber'    => $default_role
    	) );
    }

    Is there any way to add extra roles in that without editing the plugin files directly?


    Bob1nz
    Participant

    @bob1nz

    ok so I have made some small progress

    add_filter('bbp_get_user_role_map','custom_bbp_get_user_role_map');
    function custom_bbp_get_user_role_map($role_map){
        $role_map['admin'] = bbp_get_admin_role();
        $role_map['guest'] = bbp_get_guest_role();
        return $role_map;
    }

    Works if I use the built in bbpress roles eg where is says bbp_get_admin_role(); have bbp_get_keymaster_role(); , bbp_get_moderator_role(); , bbp_get_admin_role(); bbp_get_participant_role();.

    I seem to be stuck on getting the custom roles that I have created in bbpress to be picked up in the map.

    The error I get is Fatal error: Call to undefined function bbp_get_admin_role() in [pathtomyfunctions.php] on line 550 which is $role_map[‘admin’] = bbp_get_admin_role();


    Robin W
    Moderator

    @robin-w

    The bbpress function is

    $role_map[‘admin’]= bbp_get_user_role( $user_id );

    For the standard roles you will end up with

    bbp_keymaster
    bbp_moderator
    etc.

    as that’s how they’re stored in the database,


    Bob1nz
    Participant

    @bob1nz

    Thanks for your reply @robin-w
    I tried that but it assigns the user the default role in bbpress.

    I gave a dummy user the admin role in wordpress and what I want is for the user to be automatically given the admin role in bbpress as well.
    Using the code you supplied it leaves the user with the default role.

    How can I assign non-standard bbpress roles with custom capabilities to non standard wordpress roles also with custom capabilities using the map so this is performed dynamically?


    Boertje
    Participant

    @boertje

    I have the same problem here, give the plugin user role editor a try, but also doesn’t work. I want that the editors (my users) can upload a picture, but it seems impossible. Another issue is that all the users can use the HTML tab, I can t hide that tab. Why is this difference between the WP and BBPress roles? I’m very familiar and happy with WP (more than 50 installs) but bbpress is till now a very user unfriendly program for me. But maybe someone can change my mind.


    Robin W
    Moderator

    @robin-w

    @bob1nz
    How can I assign non-standard bbpress roles with custom capabilities to non standard wordpress roles also with custom capabilities using the map so this is performed dynamically?

    Sorry, but without major involvement in looking at this and how it is interacting with whatever plugins you are using, I cannot quickly come up with an answer.

    If you want me to take a professional look after xmas, contact me via my website

    http://www.rewweb.co.uk


    @boertje
    – suggest you start your own thread


    Bob1nz
    Participant

    @bob1nz

    @robin-w
    Awesome I appreciate the help and time you have given so far and will most likely contact you after the silly season is over.
    I will continue to have a play around with this for now as I feel I am close but just one little thing is missing.

    If anyone else has any ideas or knowledge on this please feel free to contribute.
    From what can gather this used to be “easy” to do prior to bbPress 2.2 when the dynamic roles were implemented. I have seen mention of some filters being able to manipulate map in some way to allow custom bbpress roles to be assigned to wordpress roles.
    However as I am not a wizard on these things, I tend to blunder around trying to make sense of it.

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