Get 15% off on all our plugins, themes and memberships till 4th December. Use coupon: BLACKFRIDAY2021.

BuddyDev

Search

[Resolved] Redirect user if not member type

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #10033

    Hi,
    How prevent user to access page if they are not member type?
    For example, if I’m not member_type[teacher] and I want access to page[work] redirect me to page[home]. 🙂
    Thank you.
    Ahmed.

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #10055

    Hi Ahmed,
    I will suggest hooking to ‘bp_template_redirect’ hook and checking for the logged in members member type using bp_has_member_type( bp_loggedin_user_id(), ‘member_type_name’ ) to redirect the users.

    If you need complete code example, please do let me know.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #10063

    hi Brajesh,
    i try 2 ways

    1st way:

    function yoostart_restriction(){
    if ( bp_has_member_type( bp_loggedin_user_id(), ‘particulier’) && is_page(‘devenir-vendeur’) ) {
    wp_redirect( home_url(‘/test’) );
    }
    }
    add_action(‘bp_template_redirect’, ‘yoostart_restriction’);

    This function works but doesn’t take users without member type.

    2nd way:

    function yoostart_restriction(){

    $member_type = bp_get_member_type( $current_user->ID );

    if (is_user_logged_in() && ‘microentreprise’ === $member_type && is_page(‘devenir-vendeur’) ) {
    wp_redirect( home_url( ‘/test’) );
    }
    }
    add_action(‘bp_template_redirect’, ‘yoostart_restriction’);

    I wanted to use variable but it doesn’t work

    thank you
    Ahmed

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #10067

    Hi Ahmed,
    your first way is the right way.

    Here is a modified version of it.

    
    
    function buddydev_example_redirect() {
    	if ( ! is_user_logged_in() ) {
    		//do something with non logged in member
    	}
    
    	$member_type = bp_get_member_type( bp_loggedin_user_id() );
    
    	if ( empty( $member_type ) ) {
    		// current user has no member type, do something
    	}
    
    	if( 'student' === $member_type ) {
    		// is student
    	} elseif( 'teacher' === $member_type ) {
    
    	} elseif( 'staff' === $member_type ) {
    
    	} else {
    		//in all other cases, do something.
    	}
    
    }
    
    add_action( 'bp_template_redirect', 'buddydev_example_redirect' );
    
    

    I hope you can take over from there.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #10078

    thank you for help Brajesh,

    Here is the function

    function yoostart_restriction() {
      if ( ! is_user_logged_in() && is_page('devenir-vendeur') ) {
        wp_redirect( home_url( '/test') );
      }
    
      $member_type = bp_get_member_type( bp_loggedin_user_id() );
    
      if ( empty( $member_type ) && is_page('devenir-vendeur') ) {
        wp_redirect( home_url( '/test') );
      }
    
      if( 'particulier' === $member_type && is_page('devenir-vendeur') ) {
        wp_redirect( home_url( '/test') );
      } 
    }
    add_action( 'bp_template_redirect', 'yoostart_restriction' );
    

    and it works well,can be improved?

    Ahmed

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #10080

    Is ‘particulier’ the only member type you want to redirect for? If yes, We can merge the last two conditions. Please let me know and I will post an update. Also, a better optimisation will be to check if ! is_page(‘page-name’) and return.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #10081

    Yes, ‘particulier’ is the only member type I want redirect.

    Ahmed.

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #10082

    Hi Ahmed,
    Please try this then.

    
    
    function yoostart_restriction() {
    
    	if ( ! is_page('devenir-vendeur') ) {
    		return ;
    	}
    
    	// we are sure we are on that page.
    	// Is user logged in?
    	if ( ! is_user_logged_in() ) {
    		bp_core_redirect( home_url( '/test') );
    	}
    
    	$member_type = bp_get_member_type( bp_loggedin_user_id() );
    
    	if ( empty( $member_type )  || 'particulier' === $member_type ) {
    		bp_core_redirect( home_url( '/test') );
    	}
    }
    add_action( 'bp_template_redirect', 'yoostart_restriction' );
    

    Let me know if it works as expected or not?

  • Participant
    Level: Enlightened
    Posts: 26
    Ahmed on #10085

    Great, it works well.
    Thank you very much Brajesh.

    Ahmed

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #10089

    You are welcome!

The topic ‘ [Resolved] Redirect user if not member type’ is closed to new replies.

This topic is: resolved