Skip to:
Content
Pages
Categories
Search
Top
Bottom

Buddypress components access to specific roles only

  • @alimalik001

    Participant

    We are in a process of making a learning management system based on Buddypress. Is there any code/ plugin by which we can restrict access to private messages, groups etc. for non students/members.

Viewing 1 replies (of 1 total)
  • @henrywright

    Moderator

    I’m not aware of a plugin that can do exactly that? Perhaps some of the guys on here know of one?

    To do it manually, you could hook a function to init and work from there. For example:

    function my_function() {
    
        if ( ! is_user_logged_in() && bp_is_current_component( 'messages' ) ) {
            // Current user is not logged in so don't let them see stuff. In this case, private messages 
            wp_redirect( home_url() );
            exit;
        }
            
        $flag = false;
        // Get current user info.
        $user = wp_get_current_user();
    
        if ( ! in_array( 'student', $user->roles ) ) {
            // The current member is not a student.
            $flag = true;
        }
    
        if ( $flag && bp_is_current_component( 'messages' )  ) {
            The currently logged-in member is not a student and is trying to view private messaging. Let's redirect them away.
            wp_redirect( home_url() );
            exit;
        }
    }
    add_action( 'init', 'my_function' );

    From that, you should be able to see how you can achieve what you’re trying to do without the need of a plugin.

    Refs:

    Template Tag Reference

    https://codex.wordpress.org/Function_Reference/wp_redirect

Viewing 1 replies (of 1 total)
  • The topic ‘Buddypress components access to specific roles only’ is closed to new replies.
Skip to toolbar