Discourse Login Redirects to WordPress Dashboard

When I originally set up SSO with WordPress, users would go to the forum URL, get redirected to the WordPress login page and after login get redirected back to the forum.

But at some point, this changed and I have no idea how. Now after the WordPress login, the user gets redirected to the WordPress dashboard.

Also, the other weird thing is that when I logout of Discourse, I see a message “You Were Logged Out” but when I click the “Refresh” button, it didn’t actually log me out. And I can still post, reply etc. I sometimes have to logout 2 or 3 times until I’m actually logged out for real. So it works, just not consistently.

Any ideas on how I can troubleshoot this?

2 Likes

There is probably a Wordpress plugin interfering with your routes and/or hooking upon your login functions. Just disable half of your Wordpress plugins and see which half causes the issue, then disable half of the plugins in that group etcetera.

Start with plugins that have something to do with authentication, memberships, login.

6 Likes

Michael’s answer is probably correct. Also, do you have the Woocommerce plugin installed on your site? If so, there is some code you can add to your site to fix the problem.

The logout issue may be related to the login redirect issue. If disabling plugins doesn’t solve the problem, let us know.

If you don’t want to disable plugins for all your users, you can disable them for your individual session with the WordPress Health Check plugin: Health Check & Troubleshooting – WordPress plugin | WordPress.org English (Canada).

4 Likes

Thanks Michael and Simon!

The login issue seems to be a conflict with the MemberPress plugin. So I guess I need to dig into a possible solution with them.

The logout issue was caused by a redirection plugin. There was a home page redirect that was causing the problem with the Discourse logout.

Michael – really appreciate your help and for the advice on plugins might be suspects. It saved me a ton of time because those were the first two plugins I checked.

Simon – thanks for telling me about the WordPress Health Check plugin. I hadn’t heard of that before but I’m going to install it now and use it for troubleshooting in future.

4 Likes

Look through the plugin’s settings for any login redirects it creates. It seems that it’s redirecting users to their profile page before the WP Discourse plugin can redirect users back to Discourse. If you find the solution, please post it here. If you don’t find the solution, let us know. I can try installing MemberPress on my local site.

Yes, the Health Check plugin is great for debugging this kind of thing.

1 Like

Hi @Simon_Cossar

I haven’t been able to get anywhere with MemberPress on solving this. It’s definitely a conflict with MemberPress because that’s the only plugin where I’ve set a specific redirect URL i.e. /jump

I’m not really sure where to go from here. When someone logs on from my WordPress site I want to redirect those people to /jump after the login. So MemberPress is doing it’s job there.

But when people access my discourse site e.g. community.mydomain.com, they get redirected to the WordPress login page but after login they also get sent to /jump instead of redirecting back to the forum.

Any ideas what else I can try here?

Thanks - Omer

3 Likes

Do you know what settings you have enabled on MemberPress to create the redirect? I have a recent version of MemberPress installed on my dev site, but have been unable to recreate either the login issue, or the issue with users not getting logged out of Discourse.

Is your WordPress site on a multisite installation, or is it just a normal WordPress installation?

It’s possible the issue you are seeing with users not being logged out of Discourse is that your Discourse site is set to require logins to view content. In this case, refreshing the Discourse page will initiate SSO login. To fully log users out of Discourse, you need to also log them out of WordPress. This can be done by entering https://example.com/?request=logout in the Discourse logout redirect site setting. Replace example.com with your WordPress site’s domain. Let me know if you’ve done this and it still isn’t working.

2 Likes

Hi Simon

It’s a normal WordPress installation (not multisite). And the logout has been fixed.

The problem is with redirect after login i.e. users don’t get redirected back to the Discourse site after the WP login. Instead it looks like MemberPress is sending them to a default page on WordPress.

–Omer

1 Like

Do you know if you have set the page that users are being redirected to in the MemberPress options? If so, what option did you set for this?

2 Likes

Yes I did. I’ve set the page redirect in two places:

  1. MemberPress > Settings > Account > “URL to direct member to after login”

  2. MemberPress > Memberships > [Membership Plan] > Advanced > “Default Login Redirect URL”

1 Like

Thanks! That is the option that is causing the issue.

2 Likes

Interesting! I need to leave that setting there for when members go to the WP site and click login.

Any idea if there’s any PHP I can add to my functions.php that might override this for Discourse login?

1 Like

The problem is happening in the track_and_override_login_redirect_mepr function. That function is hooked into with the MemberPress mepr-process-login-redirect-url filter. It may be possible to hook into this function and override it by checking for the query that have been set. This can be done with the wp_get_referer() function.

I don’t think I’ll have time to sort this out today, but I’ll take another look at it in the next few days.

2 Likes

I took another look at this while it’s fresh in my mind. Adding the following function to my theme’s functions.php file solved the issue for me, but this hasn’t been tested very much.

add_filter( 'mepr-process-login-redirect-url', 'wpdc_login_redirect', 12, 3);
function wpdc_login_redirect( $redirect, $user = false, $is_wp_login_page = false ) {
	$referer = wp_get_referer();
	if ( $referer ) {
		$query_params = [];
		parse_str( parse_url( $referer, PHP_URL_QUERY ), $query_params );
		$sso_referer = ! empty( $query_params['redirect_to'] ) && preg_match( '/^\/\?sso/', $query_params['redirect_to'] );
		if ( $sso_referer ) {

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

	return MeprProductsCtrl::track_and_override_login_redirect_mepr( $redirect, $user, $is_wp_login_page );
}

The function is hooking into the 'mepr-process-login-redirect-url' filter before it is hooked into by MemberPress. It then checks the value of the query params to see if the query was initiated by an SSO request from Discourse. If it was, it redirects the user to the WordPress homepage with the query params intact. This will cause the WP Discourse plugin to complete the SSO request. I think this can still work if your homepage is being protected by MemberPress, but it would be good to confirm that.

If the request was not initiated by Discourse, the static MeprProductsCtrl::track_and_override_login_redirect_mepr is called with the arguments that were passed to the filter.

I’m a little hesitant to recommend adding this code to your production site. If you do try it, make sure that you can remove the code from your server in case it breaks your site. Be sure to test it out will as many user/membership-levels as possible.

5 Likes

This is AWESOME Simon! You’re a rock star!

I’m also reluctant to add this to my production site right away. I think I might have to setup a test site for this properly.

3 Likes