Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allow anyone to post in forum, but only specific groups to view topics


  • Preston
    Participant

    @firebot

    WordPress Version: 4.0
    bbPress Version: 2.5.4

    I have a forum where anyone (ideally, even users without an account) will be able to post a topic, but only users in certain groups will be allowed to view and post replies to the topics.

    I’ve been working with these two plugins:
    Members: https://wordpress.org/plugins/members/
    bbPress Permissions: https://github.com/ZanderAdam/Tehnik-bbPress-Permissions

    They’ve allowed me to setup everything except the feature I described above. Google searches haven’t yielded anything either, just how to setup general user roles.

    Is it possible to do what I need to?

    I appreciate any help!

Viewing 9 replies - 1 through 9 (of 9 total)

  • Robin W
    Moderator

    @robin-w

    see

    https://codex.bbpress.org/bbpress-user-roles-and-capabilities/

    and

    Custom Capabilities

    you need to play with setting up some new roles probably one that has topic capabilities but not reply capabilities, and setting that to the default.


    Preston
    Participant

    @firebot

    I think that might get me to where I want to be. Assuming if I remove the “spectate” ability they can’t view any topics, but still post a topic.

    When I implement the second set of code (for Creating New Roles) from this page: https://codex.bbpress.org/custom-capabilities/. I get the following error in the admin area:
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘add_new roles’ not found or invalid function name in /wp-includes/plugin.php on line 214

    I thought it might be a conflict with the plugins I had installed, but disabling them didn’t solve anything. I assume the code is supposed to go into my functions.php file? That’s where I tried it anyway.

    Thanks for the help.


    Robin W
    Moderator

    @robin-w

    functions file should be fine.

    Can you post the exact code you put in the file.


    Preston
    Participant

    @firebot

    I’m just using the code copied right off the page at the moment: http://pastebin.com/qVFx300V

    Originally, I edited it to suit my needs, but I thought the error came about because of my editing.


    Robin W
    Moderator

    @robin-w

    sorry, there is a very minor error, a missing _

    add_filter( 'bbp_get_dynamic_roles', 'add_new roles', 1 );
    

    should read

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

    I’ll update the codex!


    Preston
    Participant

    @firebot

    Thanks, that did the trick. Looking back, I probably should have noticed that was the problem! It was obvious in the error message.

    Setting up the role worked, but if I set “spectate” to false, the role can’t see the forum at all to even post.

    After searching some more, I found there was a bbPress setting to allow “anonymous” posting without an account. With that enabled, and the forum set to public, I used the following code to hide the list of topics from users who aren’t logged in (I believe they always come back with an ID of 0).

    This is in the “loop-single-topic.php” file. I’ll probably also need to add this code to template files that display single topics, in case any sneaky people figure out the URL structure. The concept remains the same, however.

    <?php 
        $currentUserID = bbp_get_current_user_id(); 
        if($currentUserID == '0'){
            //Display stuff if user's ID is 0 (they aren't logged in)
        } else {
            //Put all the topic loop code here, it will display if the user is logged in (has an ID other than 0
        }
    ?>

    This seems like a pretty secure way to do it, none of the topic information is displaying in the source code or anything like that. I’ll add any other relevant information I find tomorrow. For now, it’s time to sleep.

    **EDIT**
    I’ll definitely need to set something up with regards to the single topic templates. Creating a new topic as an anonymous user still redirects you to that topic. I’ll have something tomorrow.


    Robin W
    Moderator

    @robin-w

    Hey great – thanks for posting the solution so far – it will help others reading this thread later.

    Let us know how you get on with the templates !


    Preston
    Participant

    @firebot

    I’m finally getting back around to working on this and realized I’ll need to hide the topics from a specific user role as well. I can’t just remove spectate/participate capabilities because I need them to be able to post topics, just like users who aren’t logged in.

    I’m having trouble finding a way to get the current user’s role for bbPress so I can use it in the same manner I used the user’s ID above.

    I found this function, but it returns the WordPress role (administrator, editor, etc):

    function get_user_role() {
        global $current_user;
    
        $user_roles = $current_user->roles;
        $user_role = array_shift($user_roles);
    
        return $user_role;
    }

    Can I somehow adapt it to return the bbPress role instead? (keymaster, moderator, etc.)


    Robin W
    Moderator

    @robin-w

    I’m having trouble finding a way to get the current user’s role for bbPress

    use

    bbp_get_user_role()

    in

    \bbpress\bbpress\includes\users\capabilities.php

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