Problem redirecting to forum after login when using MemberMouse

Hi Richard. It’s been a while since I worked on this and things seem to be changing from time to time in the discourse plugin (new and better functions keep cropping up) but this is what I did way back them and it still works for us.

We use the wordpress plugin membermouse. And membermouse is written in such a way that after you login, you get redirected to a specific wordpress “welcome member!” page. So this functionality was overriding discourse’s attempt to redirect you back to where you were in discourse.

membermouse has a filter called mm_login_redirect where you can change membermouse’s behavior of redirecting the user to that “welcome member!” page. Below is the php code I wrote for that filter.

// If the person came from the discourse forum, push them back after logging in
function my_mm_login_redirect( $infoObj ) {

	$current_user = $infoObj->user;
	if ( is_wp_error($current_user) ) return('');
	
	$user_id = $current_user->ID;

	if ( @$_COOKIE['detected_forum_referal'] != '' ) {

		// Payload and signature.
		$payload = @$_COOKIE['mm_cookie_sso'];
		$sig     = @$_COOKIE['mm_cookie_sig'];
		
		// Change %0B back to %0A.
		$payload = rawurldecode( str_replace( '%0B', '%0A', rawurlencode( $payload ) ) );
		
		// Validate signature.
		$sso_secret = '###################';
		$sso        = new \WPDiscourse\SSO\SSO( $sso_secret );
		if ( ! ( $sso->validate( $payload, $sig ) ) ) {
			return '';
		}
		
		$nonce  = $sso->get_nonce( $payload );
		$params = array(
			'nonce'               => $nonce,
			'username'            => $current_user->user_login,
			'email'               => $current_user->user_email,
			'external_id'         => $user_id,
		);
		
		$params = apply_filters( 'wpdc_sso_params', $params, $current_user );
		$q = $sso->build_login_string( $params );

		do_action( 'wpdc_sso_provider_before_sso_redirect', $user_id, $current_user );
		
		// Redirect back to Discourse.
		return('https://YOURFORMURLHERE' . '/session/sso_login?' . $q);
	}
	return('');
}
add_filter( 'mm_login_redirect', 'my_mm_login_redirect', 10, 1 );