# Syncing login status between WordPress and Discourse, Discourse SSO provider

**URL:** https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980
**Category:** WordPress
**Created:** [August 13, 2017, 7:56pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980 "2017-08-13T19:56:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![a9udn9u](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@a9udn9u](https://meta.discourse.org/u/a9udn9u)
#### Post date: [August 13, 2017, 7:56pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/1 "2017-08-13T19:56:09Z")

</div>

I’m using the [WP Discourse](https://github.com/discourse/wp-discourse) plugin to connect my WordPress and Discourse sites, using Discourse as the SSO provider, I plan to disable WP login page and only rely on Discourse’s user authentication. Everything worked well except login status is not synced in WordPress. Either login or logout from Discourse, my login status in WordPress doesn’t change automatically, for log in, I have to click the _Login with Discourse_ link (`https://example.com/?discourse_sso=1`). Same thing for logout, have to use WP’s logout link.

Settings in [WP Discourse](https://github.com/discourse/wp-discourse) plugin:  
Set _SSO Secret Key_  
Checked _Enable SSO Client_  
Checked _Add Login Link_  
Checked _Sync Existing Users by Email_

Settings in Discourse:  
Checked _enable sso provider_  
Set _sso secret_

WP version: 4.8.1  
[WP Discourse](https://github.com/discourse/wp-discourse) plugin version: 1.4.3  
Discourse version: 1.9.0.beta5

Is this the expected behavior? If so, what’s the the best way to enable login status syncing?

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: [August 13, 2017, 8:50pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/2 "2017-08-13T20:50:39Z")

</div>

> [@a9udn9u](#):
>
> Is this the expected behavior?

Yes, it is, but there are a couple of workarounds. You can sync logout between Discourse and WordPress by adding a URL like this to the Discourse logout\_redirect setting:  
`http://my-wp-blog.com/?request=logout`

The easiest way to get users logged in on WordPress when they visit the site from Discourse is to link to the site on Discourse with an SSO link. You can get the form of the URL by inspecting the ‘Login With Discourse’ link on your WordPress login page. It should look something like this:

`<a href="http://wordpress.dev/?discourse_sso=1&redirect_to=http://wordpress.dev/">Log in with Discourse</a>`

Change the value of the `redirect_to` parameter to get users to a specific page.

A Discourse plugin could probably be developed that would automatically sync login with WordPress. It’s not something that can be accomplished through the [WP Discourse](https://github.com/discourse/wp-discourse) plugin.

---

<div class="post-metadata">

### Author: ![a9udn9u](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@a9udn9u](https://meta.discourse.org/u/a9udn9u)
#### Post date: [August 13, 2017, 10:47pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/3 "2017-08-13T22:47:50Z")

</div>

Got it. Thanks for the explanation!

---

<div class="post-metadata">

### Author: ![Lew\_Grothe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lew_grothe/32/337776_2.png) [@Lew\_Grothe](https://meta.discourse.org/u/Lew_Grothe)
#### Post date: [July 9, 2018, 6:33pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/4 "2018-07-09T18:33:13Z")

</div>

We’re in the process of adding a WordPress front end to our Discourse forum. WordPress is client SSO to Discourse (we only log in through Discourse).

I want/need the is\_user\_logged\_in state in WordPress to be able to toggle some features on the WordPress site for folks who are logged in (and keep private from those who are not).

All works well when an individual is logged in and logs out on each machine/browser. The problem arises when a member logs in on one machine, goes to another browser or mobile device and logs in using the same account there (actually is simply identified as logged in and everything is copacetic) and then logs out there.

The original WP instance will not see the user as logged out (state for Discourse is maintained on a user level, not session).

Keeping sessions separate would probably be the best solution but seems an architectural stretch for the Discourse API.

A hack that would work for our purposes would be to simply query the Discourse server (from WP) and logout if the user has been logged out. This could be done either on a timed basis or on a page refresh on WP (e.g. in functions.php or header.php for the theme).

Am I missing something? Has someone tried something like this and willing to share their experience?

---

<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: [July 10, 2018, 2:56am UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/5 "2018-07-10T02:56:10Z")

</div>

> [@Lew\_Grothe](#):
>
> The original WP instance will not see the user as logged out

Is the basic issue you’re trying to solve that when a user is logged out on WordPress, you want them to be logged out in all WordPress sessions? [wp\_ajax\_destroy\_sessions() – Function | Developer.WordPress.org](https://developer.wordpress.org/reference/functions/wp_ajax_destroy_sessions/)

Edit: I think you could use the logic that’s in the `wp_ajax_destroy_sessions` function to write your own function that would do the same thing. The [WP Discourse](https://github.com/discourse/wp-discourse) plugin should have an action hook added to it that fires right before it calls `wp_logout`. I can get that added in the next update. You should be able to hook into that action to log users out from all sessions.

---

<div class="post-metadata">

### Author: ![Lew\_Grothe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lew_grothe/32/337776_2.png) [@Lew\_Grothe](https://meta.discourse.org/u/Lew_Grothe)
#### Post date: [July 13, 2018, 6:15pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/6 "2018-07-13T18:15:39Z")

</div>

Great idea - this would work.

Since logging out of discourse would call wp\_logout() (via logout\_redirect in the Discourse admin), couldn’t I just add an action on wp\_logout() for the user and not rely on the [WP Discourse](https://github.com/discourse/wp-discourse) plugin’s action hook?

Seems either way would work but we don’t expect ever to have a user logged into the WP instance in any other way…

---

<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: [July 13, 2018, 6:26pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/7 "2018-07-13T18:26:42Z")

</div>

> [@Lew\_Grothe](#):
>
> Since logging out of discourse would call wp\_logout() (via logout\_redirect in the Discourse admin), couldn’t I just add an action on wp\_logout() for the user and not rely on the [WP Discourse](https://github.com/discourse/wp-discourse) plugin’s action hook?

You could try that. I think that the `wp_logout` action might be called too late for this to work.

`wp_logout` is a [pluggable](https://codex.wordpress.org/Pluggable_Functions) function. If you define your own `wp_logout` function, your function will be called instead of the function defined by WordPress. You could probably create a function for this that logs the user out on all devices.

---

<div class="post-metadata">

### Author: ![Lew\_Grothe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lew_grothe/32/337776_2.png) [@Lew\_Grothe](https://meta.discourse.org/u/Lew_Grothe)
#### Post date: [August 5, 2018, 4:44pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/8 "2018-08-05T16:44:45Z")

</div>

I’m ALMOST there - thanx for all your help so far… going on our production site today…

I overwrote wp\_logout to do exactly as you said (destroy all sessions for a user) and it works the way we want with the wp\_discourse plugin now.

Since I went this far, I also solved another problem I was having to hack on the wp\_discourse plugin to solve for our community,. Our designer wanted to have existing members remain on the discourse forum when they logged (instead of the WP home page) since they’re familiar with that behavior now (pre WP front end). I had hacked the redirect in the wp\_discourse plugin to support this behavior (by a url GET parameter) but, since I was already playing with pluggable functions, I just added my code to wp\_login and now I don’t have to have any changes to wp\_discourse.

I had thought about suggesting a feature request or pull request for our hacked wp\_discourse plugin to do this but it seems to me there are VERY few folks who will want either of these behaviors as it’s outside the regular model of how login/logout works and, as with most features like this it will likely add confusion and/or support problems. So, we’re fine on the unmodified plugin for now. (you’re welcome 😀).

Thanks again for all your help and creating a great plugin for us…

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/syncing-login-status-between-wordpress-and-discourse-discourse-sso-provider/67980/9 "2024-06-08T12:37:17Z")

</div>

This topic was automatically closed after 2490 days. New replies are no longer allowed.
