Login redirect not happening the way we want

We have a range of ongoing snags in trying to allow our new members to access their desired page, after registering. All we want is to send people a link to a protected page, allow them to register (if not already) then arrive back at their desired page.

My developer has already gone through this, at some length, and it's not the first time we've discussed here (different person):

https://premium.wpmudev.org/forums/topic/membership-2-pro-redirect-user-after-registration-to-original-protected-content

  • Panos

    Hey Adam ,

    Here is a slightly modified version of the snippet which would hopefully help :

    function wpmu_membership2_remember_redirect_after_registration( $msg, $type, $obj ) {
    	if ( $msg && !is_user_logged_in() && ( $redirect_to = filter_input( INPUT_GET, 'redirect_to' ) ) ) {
    		setcookie( "membership2_redirect_to", $redirect_to, time() + DAY_IN_SECONDS, '/' );
    	}
    	return $msg;
    }
    add_filter( 'ms_model_settings_get_protection_message', 'wpmu_membership2_remember_redirect_after_registration', 10, 3 );
    
    function wpmu_membership2_save_option( $obj ) {
    	if ( !empty( $_COOKIE['membership2_redirect_to'] ) ) {
    		setcookie( "membership2_redirect", 1, time() + DAY_IN_SECONDS, '/' );
    	}
    }
    add_action( 'ms_model_member_create_new_user', 'wpmu_membership2_save_option' );
    
    function wpmu_membership2_redirect_to() {
    	$the_page = MS_Model_Pages::current_page();
    	$the_type = MS_Model_Pages::get_page_type( $the_page );
    
    	if (
    		$the_page &&
    		MS_Model_Pages::MS_PAGE_REG_COMPLETE == $the_type &&
    		!empty( $_COOKIE['membership2_redirect'] ) &&
    		!empty( $_COOKIE['membership2_redirect_to'] )
    	) {
    		$redirect_to = filter_input( INPUT_COOKIE, 'membership2_redirect_to' );
    		unset( $_COOKIE['membership2_redirect_to'] );
    		unset( $_COOKIE['membership2_redirect'] );
    		setcookie( 'membership2_redirect_to', null, -1, '/' );
    		setcookie( 'membership2_redirect', null, -1, '/' );
    		wp_redirect( $redirect_to );
    		exit;
    	}
    	return $data;
    }
    
    add_action( 'template_redirect', 'wpmu_membership2_redirect_to', 20 );

    Please let us know how it goes on your side :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.