Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Roles resetting: “— No role for these forums —”


  • kajzh
    Participant

    @kajzh

    Hello,

    I am running bbPress v2.5.12 on WordPress 4.7 with a child theme I’ve developed based on Imaginem Themes’ Sentric Theme.

    The problem:
    In my child theme’s functions, I have added the following code to make two custom roles, Professional and Member, for my Q&A/”Ask the Expert”-style forum called “Ask a Professional.” Essentially, Members can make threads asking questions, but only screened Professionals have the ability to reply and provide answers.

    I want everyone to automatically be assigned the Member role upon registration. Then I can manually “upgrade” their roles to Professional once they’ve been successfully screened. However, when a user registers and logs in, they inherit Member capabilities, but are not assigned the Member role. Manually selecting the user and assigning them the Member role doesn’t work; their role immediately reverts back to “— No role for these forums —”
    screenshot

    When I select the user and manually assign the Participant role, the role “sticks” — meaning, it doesn’t revert back to “No Role.” However, the “Member” capabilities remain.

    screenshot

    Does anyone know why this problem persists and how to fix it? This is the final thing that needs to be ironed out before my site goes live. 🙂 Thank you!

    //code to add custom roles 
     
    function add_new_roles( $bbp_roles )
    {
        /* Add a role called professional */
        $bbp_roles['bbp_professional'] = array(
            'name' => 'Professional',
            'capabilities' => custom_capabilities( 'bbp_professional' )
            );
     
        /* Add a role called member */
        $bbp_roles['bbp_member'] = array(
            'name' => 'Member',
            'capabilities' => custom_capabilities( 'bbp_member' )
            );
     
        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_professional' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_member' )
            $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 'professional' role */
            case 'bbp_professional':
                return array(
                     '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'    => true,
     
                    // Topic caps
                    'publish_topics'        => true,
                    'edit_topics'           => true,
                    'edit_others_topics'    => false,
                    'delete_topics'         => true,
                    'delete_others_topics'  => true,
                    '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'     => 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'              => 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'       => false,
                    'edit_replies'          => false,
                    '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'     => false,
                );
     
                break;
     
            default :
                return $role;
        }
    }

    Troubleshooting:

    1. I’ve tried to disable plugins and see if any of them are causing this issue. This hasn’t yielded any different results.
    2. I’ve checked with the Twenty Sixteen and Twenty Seventeen theme.
Viewing 10 replies - 1 through 10 (of 10 total)

  • Robin W
    Moderator

    @robin-w

    what is dashboard>settings>forums>auto role set to?


    kajzh
    Participant

    @kajzh

    My Auto Role is set to Member.


    Robin W
    Moderator

    @robin-w

    ok, I can’t immediately see an error in your coding

    two thoughts

    1. does professional stick – I know you don’t want to use that, but it would help show if it is the whole code or just part that is not working.

    2. since lots of people copy code, it may be that your function names are not unique – eg function add_new_roles may exist elsewhere.

    Try making them unique

    ie change

    function add_new_roles

    to

    function kajzh_add_new_roles

    and change then filter that calls that function

    add_filter( 'bbp_get_dynamic_roles', 'add_new_roles', 1 );

    to

    add_filter( 'bbp_get_dynamic_roles', 'kajzh_add_new_roles', 1 );

    and do this for all the functions – each has a name and then a filter that calls it.


    kajzh
    Participant

    @kajzh

    1. Professional doesn’t stick either. It seems like only the added roles are misbehaving. When I set my Auto Role to a prepackaged role, everything acts fine.

    2. I tried renaming the filter and the roles still don’t stick.

    Thank you for all of your help so far! If you have any other suggestions for creating new/custom roles, please let me know. This is the only modification I’m making to bbPress, so I can definitely try other approaches if any come to mind.


    Robin W
    Moderator

    @robin-w

    ok, I’ve just loaded your code to my test site, and I can set a user to member and it holds

    Suspect either theme or other plugins

    bbPress is tested with wordpress default themes. It maybe a conflict – you need to check plugins and themes

    It could be a theme or plugin issue

    Plugins

    Deactivate all but bbpress and see if this fixes. if it does, re-enable one at a time to see which is causing the error.

    Themes

    If plugins don’t pinpoint the problem, as a test switch to a default theme such as twentyfifteen, and see if this fixes.

    Then come back


    repyourhood619
    Participant

    @repyourhood619

    Hey Robin and Kajzh, did you ever get this to work? I am having the exact same problem. the custom made role is showing up under the forum role dropdown on the user profile but when the profile is saved after selecting the new role it does the default message when no role is assigned “No Role For These Forums”. Robin I did what you suggested by deactivating plugins and changing the theme but to no avail. Please see the function below.

    
    function np_add_custom_role( $bbp_roles ) {
     
    	$bbp_roles['beta_tester'] = array(
    			'name' => 'Beta Tester', 'bbpress',
                'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
    	);
    	return $bbp_roles;
    }
    add_filter( 'bbp_get_dynamic_roles', 'np_add_custom_role', 10 , 2 );

    Thanks in advanced!


    Jakobuz
    Participant

    @jakobuz

    this solved the issue for me:
    I blocked out all codes from the custom roles except for this one:

    function add_role_caps_filter( $caps, $role )
    {
        /* Only filter for roles we are interested in! */
        if( $role == 'bbp_professional' )
            $caps = custom_capabilities( $role );
     
        if( $role == 'bbp_member' )
            $caps = custom_capabilities( $role );
     
        return $caps;
    }
    add_filter( 'bbp_get_caps_for_role', 'add_role_caps_filter', 10, 2 );

    and than I was able to assign capabilities to the new roles via the members plugin of Justin Tadlock.

    I’m still wondering why this works for me … but mayby it works for you too … I hope


    akira010203
    Participant

    @akira010203

    I had the exact same issue with the creation of a new custom role.
    My old customs roles worked and still work like a charm but with the new one..

    Impossible to set it and assgin it to a user.

    The trick is simple, add your custom role with the usal way on your function.php like that :

    function vip_add_custom_role( $bbp_roles ) {
    
    $bbp_roles['vip'] = array(
    'name' => 'VIP',
    'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
    );
    
    return $bbp_roles;
    }
    
    add_filter( 'bbp_get_dynamic_roles', 'vip_add_custom_role', 1 );

    Then install “Members” Plugin from Justin Tadlock and create a new role with the exact same slug (name) as the one your created.

    You can manage the capabilites too if you want, but the most important thing is that your new custom role will be fully usable.


    Jakobuz
    Participant

    @jakobuz

    edit: when a put all the codes for the custom capabilities in a plugin (so I remove it from the functions.php), I am finally able to change the custom capabilties (by changing them in this plugin).
    Probably it’s because a plugin is loaded before the theme is loaded


    Vishnja1
    Participant

    @vishnja1

    Having the same problem. After creating new roles they aren’t saved in user profile. In the
    “Forum Role” select there is “- No role for this forums -“. Though really role seems to be applied. I’ve tried all approaches from this documentation page https://codex.bbpress.org/custom-capabilities/
    The only thing that helped was installing “Members” plugin of Justin Tadlock (thank you Jakobuz) and double creating roles with same names. I think this is a bug.

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