# Integrar Discourse com MemberMouse

**URL:** https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636
**Category:** Administrators
**Tags:** wordpress, how-to
**Created:** [2 Fevereiro , 2018 15:10 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636 "2018-02-02T15:10:00Z")
**Posts on this page:** 17
**Page:** 2

<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: [27 Fevereiro , 2018 18:16 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/21 "2018-02-27T18:16:59Z")

</div>

@pfaffman, on a related topic. For a while now, the `activate` and `deactivate` API calls haven’t been working for us. I’m using them like so:

`https://FORUM-URL/admin/users/DISCOURSE-USER-ID/activate.json?api_key=API-KEY&api_username=system`

`https://FORUM-URL/admin/users/DISCOURSE-USER-ID/deactivate.json?api_key=API-KEY&api_username=system`

Have you had any luck with these calls lately? They used to work, I believe.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [27 Fevereiro , 2018 18:22 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/22 "2018-02-27T18:22:44Z")

</div>

In my last Membermouse gig, I used only ` WPDiscourse\Utilities\Utilities::add_user_to_discourse_group( $user_id, $group_name );`, which seemed to create and activate users. I didn’t test carefully that users were being deactivated, though for that job removing users from groups, I believe was all that was required.

In [GitHub - pfaffman/discourse-user-creator: Create an activated user, optionally assigning to group · GitHub](https://github.com/pfaffman/discourse-user-creator) I `PUT` those urls to deactivate and then activate. I’m 90% sure that it was working in the last couple weeks when I worked with someone who wanted to create users, assign passwords, and activate them without user intervention.

---

<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: [27 Fevereiro , 2018 20:02 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/23 "2018-02-27T20:02:17Z")

</div>

That’s good to know. I suspect it’ll be handy to have a separate way of activating/deactivating that doesn’t involve creating a user at the same time. (Like when people’s memberships lapse and then they rejoin.) I’ll let you know if I come up with anything that works.

---

<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: [28 Fevereiro , 2018 16:27 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/24 "2018-02-28T16:27:48Z")

</div>

Lots of good info here. I’ve been able to replace some bulky API code with more direct/compact functions.

@simon, I happened to notice something. When I use the function:

`$response = WPDiscourse\Utilities\Utilities::get_discourse_user($userid);`

It works great EXCEPT if the user is deactivated in Discourse in which case it returns an error. It seems to me that you should still be able to get info about a user even if their status is deactivated.

Thanks for all your hard work on this WP/SSO/Discourse stuff!

---

<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 Março , 2018 18:46 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/25 "2018-03-01T18:46:27Z")

</div>

@pfaffman, that activate/deactivate API does actually seem to work after all. But oddly I also get an AJAX error which causes a bit of an issue for us:

```
"You are not permitted to view the requested resource."
"invalid_access"

```

Have you found any other way to activate/deactivate? I tried `sync_sso_record` passing in `'active' => 'false'` but no dice. Also tried `'active' => 0`.

Just curious.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [1 Março , 2018 19:01 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/26 "2018-03-01T19:01:31Z")

</div>

> [@lkramer](#):
>
> Have you found any other way to activate/deactivate?

No, but if you log them out of Discourse (there’s some path to do that) and they can’t log in through WordPress, deactivation shouldn’t be necessary, as they can’t log in, which is the point, right?

The users that I added with the `sync_sso_record` were all activated, so that shouldn’t be a problem either. I think.

---

<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 Março , 2018 19:56 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/27 "2018-03-01T19:56:27Z")

</div>

Canceling their WordPress account doesn’t log them out of Discourse so theoretically they could keep using Discourse as long as their Discourse login cookie stays in place. But I think you’re right that there is some way do purposely log them out of Discourse upon cancellation.

But more importantly to me is they will continue to get Discourse’s emails (Summary emails, Private Message notifications, @username mentions, etc.) unless you deactivate them in Discourse. So there needs to be some sort of proactive way to deactivate them.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [1 Março , 2018 20:00 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/28 "2018-03-01T20:00:44Z")

</div>

> [@lkramer](#):
>
> But more importantly to me is they will continue to get Discourse’s emails

Oops. Right. I probably need to fix that. I just hope I can remember who it was for. 😉

@simon, is there a deactivate hook in wp-discourse, or do we need to do this with an API call?

---

<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 Março , 2018 20:05 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/29 "2018-03-01T20:05:47Z")

</div>

There’s no way to deactivate a Discourse user through the plugin. It looks like a `PUT` request to `/admin/users/$user_id/deactivate` should do the trick.

---

<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: [3 Março , 2018 01:12 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/30 "2018-03-03T01:12:58Z")

</div>

I tested this out. It is returning deactivated users for me. What it won’t return are users who don’t have a `SingleSignOnRecord` associated with them. If you have created a user through the API, and that user has never logged into Discourse, they won’t be returned. If you pass the `true` parameter to the function after the `user_id` parameter, it will try to look up the user by their email address if it can’t find them by their `external_id`. That should work for all users. The problem with doing this is that it’s making 2 API calls.

A better solution is to use the newly rewritten `add_user_to_discourse_group` function, or the `sync_sso_record` function, for creating users. It’s more efficient, and you can be sure that the user has an `external_id` associated with them.

---

<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: [4 Junho , 2018 13:26 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/31 "2018-06-04T13:26:37Z")

</div>

> [@lkramer](#):
>
> Step 5: Auto-redirect back to Discourse when appropriate

For anyone reading this, a much simpler way to do the auto-redirect is spelled out in the 14th post in this thread:

> [@Problem redirecting to forum after login when using MemberMouse](https://meta.discourse.org/t/problem-redirecting-to-forum-after-login-when-using-membermouse/77871/14):
>
> 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 paramet…

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [17 Março , 2019 14:13 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/32 "2019-03-17T14:13:46Z")

</div>

@lkramer, thank you for this guide and thread. I am attempting to configure MM and Discourse today but have a basic question.

The sales page for my forum is at `domain.com/forum` however Discourse is installed at `forum.domain.com`

Can I use MemberMouse on my Wordpress site (`domain.com`) to grant Access to the Discourse install on the forum subdomain? The forum subdomain only has Discourse, nothing else (different server).

Where do I need to authorize the MM plug-in to work? I assume `domain.com`

Thank you!

---

<div class="post-metadata">

### Author: ![bts](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bts/32/184556_2.png) [@bts](https://meta.discourse.org/u/bts)
#### Post date: [17 Março , 2019 19:55 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/33 "2019-03-17T19:55:12Z")

</div>

Hey Casey, that setup (forum at `forum.domain.com` and main site at `domain.com`) sounds pretty standard, so I think installing and configuring [WP Discourse](https://github.com/discourse/wp-discourse) + the above guide for syncing up w/ users’ memberships should should get you most of the way there.

I’ve done a similar setup with WP and I think this should work well for you, though I don’t have experience with MemberMouse specifically. If there’s something slightly different you’re trying to do from everything described above, or you get stuck with something along the way, holler!

---

<div class="post-metadata">

### Author: ![waffleslop](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/waffleslop/32/244974_2.png) [@waffleslop](https://meta.discourse.org/u/waffleslop)
#### Post date: [18 Março , 2019 03:06 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/34 "2019-03-18T03:06:01Z")

</div>

What I’m understanding is that the SSO function is really what gives MM + Discourse it’s link.

When a prospect buys membership via MM, a new WP user is created and then the functions above create a new Discourse user. Similarly, when a membership lapses (non-payment), MM deactivates the WP user and the functions above deactivate the Discourse user.

I’ve gotten SSO configured and created my own plugin for the functions to sit inside.

I’ve run into a few snags. In @lkramer’s post, she mentions the following in Step 2:

> So you need add this line to the file **/lib/discourse-sso.php** in public function \_\_construct( $wordpress\_email\_verifier ):

I cannot find that file. Here’s what I’m seeing:

 ![image](https://global.discourse-cdn.com/meta/original/3X/e/3/e3544b1df06af972810eb144592b41212c0720cd.png)

(Note: the lib/sso folder is empty)

I did find lib/email-notification.php, which has this line:  
/\*\*  
\* EmailNotification constructor.  
\*/  
public function \_\_construct() {  
add\_action( ‘init’, array( $this, ‘setup\_options’ ) );  
}

Is it necessary to change this line? I assume I need to add this new “add\_action” from Leah here, then add the scripts below to functions.php, but am not sure that script should go inside this “EmailNotification” bit.

* * *

Next major question: Inside Step 4, there are 4 bullets (Use the Discourse API to get the user’s Discourse username), Map their MM Membership ID to equic Discourse group ID, Sync up their user/email if they get changed in WO, Activate/deactivate Discourse users depending on status in MM).

I have a brand new site without any users, aside from Admin.

This seems quite involved. I’m getting outside of my comfort zone (ha! I’ve been outside it for awhile now!).

Questions:  
1.) Is anyone able to share how they have these four pieces configured?  
2.) Is the #Marketplace the right place for me to post to hire someone to do this?

Thanks for all the help. Y’all are the best.

---

<div class="post-metadata">

### Author: ![Julien\_Kmeo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julien_kmeo/32/132955_2.png) [@Julien\_Kmeo](https://meta.discourse.org/u/Julien_Kmeo)
#### Post date: [3 Abril , 2020 18:18 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/35 "2020-04-03T18:18:52Z")

</div>

Ei, Casey, encontrei o discourse-sso.php na pasta sso-provider.

Mas, para ser honesto, eu também adoraria ajuda para configurar a Etapa 4… 😅

---

<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: [3 Abril , 2020 18:31 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/36 "2020-04-03T18:31:46Z")

</div>

> [@lkramer](#):
>
> Descobri que, para que o wp-discourse realmente crie um usuário no Discourse quando um usuário é criado no WordPress, foi necessário fazer uma alteração de código no plugin. Isso ocorre porque o plugin depende da ação “wp\_login”, mas ela se comporta de maneira diferente no MemberMouse em comparação ao comportamento padrão do WordPress. Portanto, você precisa adicionar esta linha ao arquivo /lib/discourse-sso.php dentro da função pública.

Devo ter perdido essa informação ao ler o guia pela primeira vez. Em algumas circunstâncias, pode fazer sentido editar diretamente os arquivos de um plugin, mas não recomendo isso para a maioria dos sites que usam o plugin [WP Discourse](https://github.com/discourse/wp-discourse) — especialmente alterações relacionadas ao login SSO. A melhor abordagem para estender um plugin é confiar nos ganchos de ação e filtro existentes no plugin. Vou adicionar ganchos ao plugin se isso evitar que as pessoas sobrescrevam o código do plugin. Estou me perguntando se há algum gancho que possa ser adicionado ao plugin aqui, para que as pessoas não precisem sobrescrever o código do plugin.

---

<div class="post-metadata">

### Author: ![Muhammad\_Kashif](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muhammad_kashif/32/343955_2.png) [@Muhammad\_Kashif](https://meta.discourse.org/u/Muhammad_Kashif)
#### Post date: [27 Novembro , 2023 05:54 UTC](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636/37 "2023-11-27T05:54:53Z")

</div>

@lkramer seria possível compartilhar o código das funções personalizadas que você criou?

add\_action(‘mm\_member\_membership\_change’, ‘run\_discourse\_sync\_based\_on\_mm\_acct\_change’);  
add\_action(‘mm\_member\_status\_change’, ‘run\_discourse\_sync\_based\_on\_mm\_acct\_change’);  
add\_action(‘mm\_member\_account\_update’, ‘run\_discourse\_sync\_based\_on\_mm\_acct\_change’);

[Previous page](https://meta.discourse.org/t/integrate-discourse-with-membermouse/79636.md?page=1)
