# Problem redirecting to forum after login when using MemberMouse

**URL:** https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871
**Category:** WordPress
**Created:** [1월 12, 2018, 7:22오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871 "2018-01-12T19:22:17Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [1월 12, 2018, 7:22오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/1 "2018-01-12T19:22:17Z")

</div>

Hello. So in searching some other threads here, it seems like the expected behavior is this:

1. Someone is reading a thread in Discourse and decided to login so they click the Login button
2. They pop over to WordPress where they login
3. They are redirected back to the Discourse thread that they were reading

That sounds great! In our case, this isn’t working I think because we use the MemberMouse plugin which forces people to a certain “welcome” URL after logging in. BUT, I can bypass that with a filter MemberMouse provides.

The only problem, is that the URL of the Discourse topic the person was reading doesn’t seem to be passed to WordPress.

E.g. Let’s say I’m in this topic in Discourse:  
`https://forums.gemsociety.org/t/any-brazil-company-which-sold-rough-faceting-amethyst-citerine-and-amertine/1246`

And I click the Login button in Discourse

I get sent to this URL:  
`https://www.gemsociety.org/login/?redirect_to=%2Flogin%3Fsso%3Dbm9uY2U9Y2IwZGE0NmRlMGM0YTlhOGM1OTc4OGU0NGRjOGU3MDkmcmV0dXJu%250BX3Nzb191cmw9aHR0cCUzQSUyRiUyRmZvcnVtcy5nZW1zb2NpZXR5Lm9yZyUy%250BRnNlc3Npb24lMkZzc29fbG9naW4%253D%250B%26sig%3D419ba4dfda93d068c81470cf32e4aec633217047728a9bca514bbae3891cffde`

Nowhere in that big string do I find the URL the person was reading in Discourse (/any-brazil-company…/).

So, I’m a little confused here. Any thoughts? Thanks!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [1월 12, 2018, 7:37오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/2 "2018-01-12T19:37:57Z")

</div>

> [@lkramer](#):
>
> The only problem, is that the URL of the Discourse topic the person was reading doesn’t seem to be passed to WordPress.

The sso return path is saved on Discourse as the value of the nonce key. If you can find a way to get your WordPress site to redirect back to Discourse (`/session/sso_login?`) with the sso params users should get redirected to the page they clicked the login link on.

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [1월 12, 2018, 8:28오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/3 "2018-01-12T20:28:23Z")

</div>

Thanks Simon for the reply. So, should I redirect the user to this?

`https://forums.gemsociety.org/session/sso_login?sso=bm9uY2U9ZTU3NWU3YzI2ODM2MjEzYjNmZTNmZWE5ZTc4NjNiNTQmcmV0dXJu%250BX3Nzb191cmw9aHR0cCUzQSUyRiUyRmZvcnVtcy5nZW1zb2NpZXR5Lm9yZyUy%250BRnNlc3Npb24lMkZzc29fbG9naW4%253D%250B%26sig%3D9c9ee72cd56218de3fb5ba46b985084369874fc5f434afdb1d9b5d14d170c0a`

When I do this, I get a blank screen in Discourse.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [1월 12, 2018, 8:50오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/4 "2018-01-12T20:50:18Z")

</div>

Right, that’s not going to work. If you look here: [wp-discourse/lib/sso-provider/discourse-sso.php at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/blob/master/lib/sso-provider/discourse-sso.php#L146) you can see what needs to happen. If the `sso` and `sig` parameters are in the query\_vars and a user is not logged in, they are redirected to the WordPress login URL with the SSO request from Discourse set as the redirect parameter. It seems like the MemberMouse plugin is overriding that redirect.

Somehow, you need to preserve the redirect (the parameter from this line: `$login = wp_login_url( esc_url_raw( $redirect ) );` and redirect to it after MemberMouse has done its work. The request should then be processed by the part of the `sso_parse_request` function that begins here:

> <https://github.com/discourse/wp-discourse/blob/main/lib/sso-provider/discourse-sso.php#L188>

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [1월 22, 2018, 2:21오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/5 "2018-01-22T14:21:04Z")

</div>

I got it working! Thanks very much @simon.

Now when someone comes into the forum; clicks the blue Login button; logs into wordpress by typing in their username/pw; they are redirected back to thread they were at in Discourse.

I think this is going to be a huge win for people who come in via the Discourse emails e.g. and want to participate in the thread. Previously, they’d lose track of where they were since they didn’t get redirected back to the thread.

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [1월 22, 2018, 2:28오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/6 "2018-01-22T14:28:55Z")

</div>

P.S.

For anyone else looking into this – This “redirecting back to where you were” behavior works (thx to the WP-Discourse plugin) in the **scenario #1 where you are already logged into WordPress**.

But it’s **scenario #2 where you need to login to WordPress** that you are not automatically redirected back to the forum thread where you were. This is what I added in myself thanks to @simon 's help.

I thought it was due to my membership plugin but I actually just think the scenario #2 redirect functionality doesn’t exist in the WP-Discourse plugin. I could be wrong about this, feel free to correct me!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [1월 22, 2018, 4:19오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/7 "2018-01-22T16:19:42Z")

</div>

> [@lkramer](#):
>
> I thought it was due to my membership plugin but I actually just think the scenario #2 redirect functionality doesn’t exist in the WP-Discourse plugin.

Yes, WordPress plugin has that functionality, but other plugins that do login redirects can break it.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [5월 29, 2018, 3:58오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/8 "2018-05-29T15:58:52Z")

</div>

> [@lkramer](#):
>
> But it’s **scenario #2 where you need to login to WordPress** that you are not automatically redirected back to the forum thread where you were. This is what I added in myself thanks to @Simon\_Cossar 's help.

Could you please share what you exactly have done to fix this?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [5월 29, 2018, 5:06오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/9 "2018-05-29T17:06:52Z")

</div>

When SSO is enabled, clicking the login link on Discourse should automatically redirect users back to the Discourse page that they clicked the login link on. If this isn’t working for you, can you give some details about where you are seeing the problem?

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [5월 29, 2018, 6:52오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/10 "2018-05-29T18:52:58Z")

</div>

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 );

```

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [5월 29, 2018, 8:10오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/11 "2018-05-29T20:10:33Z")

</div>

Thank you so much!!  
We’ve been struggling with this MemberMouse behavior all day and this seems to be the perfect solution! 🙂

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [5월 29, 2018, 8:11오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/12 "2018-05-29T20:11:52Z")

</div>

Hey @lkramer, you included a `$sso_secret` in the code above. We’ve removed it, but if that is a valid `$sso_secret` you should change it.

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [5월 29, 2018, 10:59오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/13 "2018-05-29T22:59:41Z")

</div>

(Oops. Thx all sorted out.)

Btw, I should reiterate that there may be a better way to accomplish than my code above. It’s just what worked at the time that I needed to solve this issue.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [5월 30, 2018, 2:50오전 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/14 "2018-05-30T02:50:29Z")

</div>

Thanks for doing this! I installed the Membermouse plugin in my development environment today. Getting the redirect back to Discourse is harder than I expected. I don’t think there’s any reason to change the way you are doing it, but here’s an alternate approach. It at least shows how the plugin expects things to work. Generally, it should be possible to do this by hooking into the WordPress `login_redirect` filter and getting the Discourse `redirect_to` parameter from that filter’s `$request` parameter. This can’t be done with Membermouse though.

This function depends on being able to get the `HTTP_REFERER` with `wp_get_referer`. The referer may not always be available. If it isn’t, the user will be redirected to the default Membermouse redirect. I have only tested this in my development environment.

```php
add_filter( 'mm_login_redirect', 'wpdc_login_redirect_override' );
function wpdc_login_redirect_override() {
    $referer = wp_get_referer();
    if ( $referer ) {
	    $query_params = [];
	    parse_str( parse_url( $referer, PHP_URL_QUERY ), $query_params );
	    if ( isset( $query_params['redirect_to'] ) ) {

		    return home_url( $query_params['redirect_to'] );
	    }
    }

	return '';
}

```

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [5월 30, 2018, 1:24오후 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/15 "2018-05-30T13:24:10Z")

</div>

@simon, I did a test and that function you provided definitely works. So much more compact and efficient. Thanks! (@RGJ you should definitely give that function a try.)

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [6월 5, 2018, 11:58오전 UTC](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/16 "2018-06-05T11:58:27Z")

</div>

> [@lkramer](#):
>
> @RGJ you should definitely give that function a try.)

Works like a charm indeed! Thank you @simon
