Membership 2 redirect after login based on membership

I have two memberships and there will be more membership levels in the future. I want to redirect users after they are logged in to custom URLs (one of the url BuddyPress profile page). There is Redirect add-on but, I want to redirect users to a different page based on membership they have. How can I achieve that?

  • Paul Kevin
    • Neo

    Hey there Alexander ,

    Hope you are well today. If you are using the Membership ajax login, you can use the filters as below :

    add_filter( 'ms-ajax-login-redirect', 'my_membership_custom_redirect', 10, 2 );
    add_filter( 'ms_url_after_login', 'my_membership_custom_url_redirect', 10, 2 );
    
    function my_membership_custom_redirect( $redirect, $userid ) {
    	$membership_id = 1;
            $second_membership_id = 2
    	$member = MS_Model_Member::get_current_member();
    	if ( $member->has_membership( $membership_id ) ) {
    		return 'https://www.google.com';
    	} else if ( $member->has_membership( $second_membership_id ) ) {
                  return 'https://www.facebook.com';
            }
    	return $redirect;
    }
    
    function my_membership_custom_url_redirect( $redirect, $enforce ) {
    	$membership_id = 1;
            $second_membership_id = 2
    	$member = MS_Model_Member::get_current_member();
    	if ( $member->has_membership( $membership_id ) ) {
    		return 'https://www.google.com';
    	}  else if ( $member->has_membership( $second_membership_id ) ) {
                  return 'https://www.facebook.com';
            }
    	return $redirect;
    }

    Remember to change the value of $membership_id to the membership you want and to set conditions based on the memberships you want to check and to set the correct redirect urls.

    Warm Regards
    Paul Kevin

  • Alexander
    • WPMU DEV Initiate

    Thanks Paul,

    I'm doing great. I hope you are as well.

    Where would I find the Membership Ajax login? I am using another login plugin, but I will happily replace it with the Membership Ajax plugin.

    I know my way around WordPress to an extent, but I'm not that advanced technically. So any guidance you can provide to me would be greatly appreciated.

    Thanks,
    Alex

    • Paul Kevin
      • Neo

      Hey there Alexander ,

      You could try add this piece of code to your theme's functions.php file

      add_filter( 'login_redirect', function( $redirect_to, $request, $user){
      
          if( $user instanceof WP_User ){
      
              $memberships_and_urls = array(
                  14 => 'http://site.com/pageone',
                  22 => 'http://site.com/pageone',
              );
      
              $member = MS_Factory::load( 'MS_Model_Member', $user->ID );
      
              foreach( $memberships_and_urls as $membership_id => $url ){
      
                  if ( $member->has_membership( $membership_id ) ) {
                      $redirect_to = $url;
                      break;
                  }
      
              }
          }
      
          return $redirect_to;
      
      }, 20, 3 );

      Where the 14 and 22 are the membership ids which correspond with the url you want to redirect to.

      Warm Regards
      Paul Kevin

  • Alexander
    • WPMU DEV Initiate

    Thanks Paul,

    I appreciate your help with this. I'll look to add this ASAP and let you know if I come across any issues and ask for your guidance. If all goes well and I add it with any issues, I'll let you know as well.

    Best regards,
    Alex

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.