Need help with registration flow in Membership 2 Pro

Someone registers on the site and receives the confirmation email. The confirmation email should have a link that will confirm the user and will redirect to a page where the user can select a membership type (we will have more membership types in the future)

Please help me with it.

  • Kris

    Hi Edwin

    Hope you are doing good today.

    By default if you have enabled "Enable account verification" in Membership users gets emails with link to verify their accounts - which is custom login page generate by %verification-url%.

    In Membership there is add on called " Redirect Control" which allow to redirect user after log in. However it does not cover login form on login page generated by %verification-url%.

    I flagged this for our SLS Team (code experts) so that they can check this and provide some solution for you. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

  • Panos

    Hi there Edwin ,

    and apologies for delay.

    In order to verify the user, it needs to land on that page, so we can't change that link. We could redirect directly after reaching that page, but user is not logged in at that point. So if we redirect to memberships list then, the user will have to log in again. So here is what I would suggest as a flow.

    After the registration, member clicks on the verification link which links to the accounts page. Since he is not logged in it will show the login form, which is what we need. Now here is where we can handle the redirect, after the login happens. We can monitor if member has chosen a membership or not. (If he uses Membership registration form, he first needs to choose a membership and then register).

    If he has chosen no membership then we can redirect to memberships list page (not sure how this case is possible as verification email is sent from Membership registration process. You can sent the verification email from admin manually in case you want to test). If he has a membership we can check if it is free or not.

    If it is free it will add that membership to user and redirect to accounts page. If it's paid, it will redirect to checkout page in order to pay for that membership.

    If you agree with this flow I described, you can add the following snippet in a mu-plugin :

    add_filter( 'ms_view_shortcode_login_data', function( $data ){
    
    	if ( ! isset( $_GET['action'] ) || 'ac' != $_GET['action'] || ! isset( $_GET['key'] ) || empty( $_GET['key'] ) ) {
    		return $data;
    	}
    
    	$verification_key 	= wp_unslash( $_GET['key'] );
    	$user_id 			= MS_Model_Member::verification_account_id( $verification_key  );
    
    	if ( $user_id ) {
    
    		$redirect_login = get_user_meta( $user_id, '_ms_user_activation_redirect_url', true );
    
    		if ( $redirect_login ) {
    			$data[ 'redirect_login' ] = $redirect_login;
    		}
    
    	}
    
    	return $data;
    
    } );
    
    add_action( 'ms_account_verification_key', function( $username, $key ){
    
    	$user = get_user_by( 'login', $username );
    	$member = MS_Factory::load( 'MS_Model_Member', $user->ID );
    	$redirect_url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_MEMBERSHIPS );
    
    	//If a membership has been selected from member
    	if ( isset( $_REQUEST[ 'membership_id' ] ) ) {
    
    		$membership_id = intval( $_REQUEST[ 'membership_id' ] );
    		$membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    
    		if ( $membership->is_free() ) {
    			// 1. If free, assign and redirect to acocounts page (on login)
    
    			$subscription = $member->add_membership( $membership->id, '' );
    			$subscription->add_payment( 0, MS_Gateway_Free::ID, 'free' );
    
    			$redirect_url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT );
    
    		}
    		else {
    			// 2. If Paid, redirect to checkout page
    
    			$redirect_url = esc_url_raw(
    							add_query_arg(
    								array(
    									'step' => MS_Controller_Frontend::STEP_PAYMENT_TABLE,
    									'membership_id' => $membership_id,
    								),
    								MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER )
    							)
    						);
    
    		}
    
    	}
    
    	update_user_meta(
    		 $user->ID,
    		'_ms_user_activation_redirect_url',
    		$redirect_url
    	);
    
    }, 20, 2 );

    In case you not familiar with mu-plugins you can read about them here :
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    Hope this helps!

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.