BuddyDev

Search

[Resolved] Restrict access to Buddypress if no member type

  • Participant
    Level: Initiated
    Posts: 2
    Gavin on #27529

    Hi!

    I’m using WooCommerce Memberships with Member Types Pro.

    Users on my site have the member type ‘Member’ assigned to them when they subscribe via WooCommerce.

    What I’d like to do is hide Buddypress from logged in users who do not have a Member Type assigned to them – these will typically be users who have an expired membership and have reverted to the WooCommerce role of ‘Customer.

    Is there a way that this can be done, either based on the user’s role, or lack of a Member Type?

    Many thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #27534

    Hi Gavin,
    Thank you for the topic.

    Yes, It is doable with a few lines of code.

    Can you please help me understand how do you perceive hiding BuddyPress? What features do we restrict fro the user?

    Please let me know the features we need to restrict(example:- do not allow access to profile) and I will assist.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Gavin on #27537

    Thanks Brajesh.

    The aim is to prevent access to:
    – activity stream
    – member list
    – user profiles

    I’ve been able to hide the menu items for the relevant users, but the pages are still directly accessible via the URL due to the limitations of woocommerce memberships.

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #27551

    Hi Gavin,
    Thank you.

    Please use the following code.

    
    
    // Check access for user.
    add_action( 'bp_template_redirect', function () {
    
    	if ( ! is_user_logged_in() || is_super_admin() ) {
    		return;
    	}
    
    	// User has somemeber type?
    	if ( bp_get_member_type( bp_loggedin_user_id() ) ) {
    		return;
    	}
    
    	// redirect as user does not ahve any member type.
    	$redirect_to = site_url( '/' ); //change with where you want to redirect. Make sure it is outside buddypress.
    
    	if ( is_buddypress() ) {
    		// if we are in BuddyPress section, redirect.
    		bp_core_redirect( $redirect_to );
    	}
    } );
    
    

    It redirects user to site home page if they try to access any BuddyPress content.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Gavin on #27555

    Worked perfectly, many thanks Brajesh!

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #27565

    Thank you!

The topic ‘ [Resolved] Restrict access to Buddypress if no member type’ is closed to new replies.

This topic is: resolved