Preston (@firebot)

Forum Replies Created

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

  • 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.)


    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.


    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.


    Preston
    Participant

    @firebot

    One last follow up for anyone who comes across this later on.

    For the select drop downs on the edit page, I decided to “cheat” with jQuery to make sure the correct value is still selected.

    This is the code in my functions.php file that creates the select input:

    $play_os = get_post_meta( $topic_id, 'bbp_app_character_playos', true);
    echo '<br><label for="bbp_app_character_playos">Select Input Label</label><br>';
    echo '<select id="bbp_app_character_playos" name="bbp_app_character_playos" data-value="' . $play_os . '">
        <option value="Yes" class="os-yes">Yes</option>
        <option value="No" class="os-no">No</option>
    </select>';

    I’m displaying the current value in the data-value attribute so I can use it in the jQuery below:

    function appEditValues(){
        var playOsVal = jQuery('.topic-edit #bbp_app_character_playos').attr('data-value');
        if(playOsVal == "Yes"){
            jQuery('.topic-edit #bbp_app_character_playos').find('.os-yes').attr('selected','selected');
        } else if (playOsVal == "No"){
            jQuery('.topic-edit #bbp_app_character_playos').find('.os-no').attr('selected','selected');
        }
    }

    The jQuery code adds the “selected” attribute to the appropriate option on the edit page, which of course causes that value to be selected and save correctly again when you finish editing.


    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.


    Preston
    Participant

    @firebot

    Thanks, I got it figured out as far as text fields keeping content when editing.

    I changed the start of my bbp_applications_fields to the following (in case anyone comes accross this thread):

    function bbp_application_fields() {
    	$forum_id = bbp_get_forum_id();
    	$application_forum_id = 34;
    	$topic_id = bbp_get_topic_id();
    	$topic_edit_id = bbp_get_topic_forum_id( $topic_id );
    	if( $forum_id == $application_forum_id || $topic_edit_id == $application_forum_id ) {

    My select fields still don’t retain their content when editing, but I suspect that’s due to some jQuery I’m using to show/hide certain options based on other select boxes.

    **EDIT**
    It looks like none of the select fields come back with their saved value, just whatever the starter value is. I’ll keep playing around with it.

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