# Memberpress + WP Discourse (Groups Management)

**URL:** https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447
**Category:** WordPress
**Tags:** sso
**Created:** [September 8, 2022, 11:44am UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447 "2022-09-08T11:44:37Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![SweaterVest](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sweatervest/32/182737_2.png) [@SweaterVest](https://meta.discourse.org/u/SweaterVest)
#### Post date: [September 8, 2022, 11:44am UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/1 "2022-09-08T11:44:37Z")

</div>

I’ve been lurking in the meta.discourse forums for years, grabbing snippets and learning how to improve a client site that uses Wordpress + Memberpress + Discourse (self-hosted).

I needed (and still do need) to assign Discourse Groups based on Memberpess Memberships.

For reference:

- The client site (SSO provider): [https://secretsofsolo.com/](https://secretsofsolo.com/)
- The Discourse site: [https://jamcircle.secretsofsolo.com/](https://jamcircle.secretsofsolo.com/)

**Previously (for a few years) I have used** `add_filter( 'wpdc_sso_params'` **filter method:**

> [@Bring over permission level from WordPress MemberPress](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/16):
>
> @simon Thanks, Simon, indeed, my text editor gives curly quotes, confused unfortunately the code didn’t help. but… not giving up, slight_smile EDIT! It DID work, i had made an error in matching the names between Groups and WP users. YEEHA!!! couldn’t be happier!! BUT… they now get added to ALL groups. so i will have to add the php one-by-one, each to their corresponding group. like: 47281 leads to “Forever Free” the next two lead to “Creative License” etc. RE-EDIT: I’ve inserted t…

The downside seemed to be that the Discourse Groups would only update upon user login, I wanted something that would have a more immediate effect (for example when a Memberpress transaction happens).

Admittedly, my own PHP coding skills are quite basic. I’m more fronted HTML/CSS developer.

Now I have rewritten my functions for the client site using a better [WP Discourse](https://github.com/discourse/wp-discourse) function described here

> [@Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse-sso/74724):
>
> [WP Discourse](https://github.com/discourse/wp-discourse) functions The [WP Discourse](https://github.com/discourse/wp-discourse) plugin has a couple of functions that can be used to associate WordPress users with Discourse groups. These functions are only enabled on sites that are using WordPress as the SSO provider for Discourse. add\_user\_to\_discourse\_group( $user\_id, $group\_names ) [View Code](https://github.com/discourse/wp-discourse/blob/main/lib/utilities.php#L278-L301) remove\_user\_from\_discourse\_group( $user\_id, $group\_names ) [View Code](https://github.com/discourse/wp-discourse/blob/main/lib/utilities.php#L303-L326) Each of these functions take the same two parameters: $user\_id: the WordPress user’s id $group\_names: a comma separat…

Which uses the: `\WPDiscourse\Utilities\Utilities::add_user_to_discourse_group` and: `\WPDiscourse\Utilities\Utilities::remove_user_from_discourse_group` functions.

**Now I can listen to Memberpress Events and add the Discourse Groups with this code block (functions.php):**

```plaintext
add_action('mepr-event-create', 'listen_to_mepr_events');
function listen_to_mepr_events($event) {
  $obj = $event->get_data();
  //$obj might be a MeprTransaction object or a MeprSubscription object

  if(!($obj instanceof MeprTransaction) && !($obj instanceof MeprSubscription)) {
    return; // nothing here to do if we're not dealing with a txn or sub
  }

  $member = $obj->user(); // get member object data from event object

  $user_id = $member->ID; // get user ID from object

  if($member->is_active_on_membership($obj)) { //active membership
    
    if(3780 == $obj->product_id) { //MONTHLY Membership
        $add_group = 'Ksenia_Basic'; // Discourse Group Name
        $remove_groups = ['Ksenia_Essential','Ksenia_Premium','Ksenia_Free','Ksenia_Annual'];
        // add to Discourse Group
        \WPDiscourse\Utilities\Utilities::add_user_to_discourse_group( $user_id, $add_group );
        // remove from Discourse Groups
        \WPDiscourse\Utilities\Utilities::remove_user_from_discourse_group( $user_id, $remove_groups );
    }
    else if(3847 == $obj->product_id) { //6 Month (Essential) Membership
        $add_group = 'Ksenia_Essential'; // Discourse Group Name
        $remove_groups = ['Ksenia_Basic','Ksenia_Premium','Ksenia_Free','Ksenia_Annual'];
        // add to Discourse Group
        \WPDiscourse\Utilities\Utilities::add_user_to_discourse_group( $user_id, $add_group );
        // remove from Discourse Groups
        \WPDiscourse\Utilities\Utilities::remove_user_from_discourse_group( $user_id, $remove_groups );
    }
  }
  else { //no matches
  }
}

```

Additional reference: The Memberpress action hook `add_action('mepr-event-create', 'listen_to_mepr_events');` example can be found here:

> **[Action Hooks in MemberPress | MemberPress](https://memberpress.com/docs/action-hooks-in-memberpress/)**
>
> Here you can find the list of the most common action hooks divided into sections that you can find and use in MemberPress. If you want to learn more about hooks please check outActions and Filters in MemberPress article. Checkout Form...

**FYI - This is a simplified version of what I am using on my client site. The full version (which adds in Learndash + Mailster management) can be found here:** [Memberpress + Learndash + Mailster + WP Discourse · GitHub](https://gist.github.com/abmiller99/409a3acee809bcfd40a389e040f062c2)

Thanks to everyone in this forum who provided snippets and support over the years. I know there are many lurkers like me who find this an extremely valuable and helpful resource.

If you have any suggestions or see problems with my example, let me know how I can improve.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [September 8, 2022, 1:07pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/2 "2022-09-08T13:07:35Z")

</div>

Thanks Andrew, I’ll take a look at this tomorrow and give you a more detailed response on Monday. Just as an initial note, have you had a look at the Memberpress group sync plugin developed by @fzngagan ?

[https://github.com/paviliondev/wp-discourse-mepr-sync](https://github.com/paviliondev/wp-discourse-mepr-sync)

---

<div class="post-metadata">

### Author: ![SweaterVest](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sweatervest/32/182737_2.png) [@SweaterVest](https://meta.discourse.org/u/SweaterVest)
#### Post date: [September 8, 2022, 3:15pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/3 "2022-09-08T15:15:44Z")

</div>

Role based - nice 🕶 No, I had not seen that plugin. Excellent contribution.

---

<div class="post-metadata">

### Author: ![earlysound](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/earlysound/32/319316_2.png) [@earlysound](https://meta.discourse.org/u/earlysound)
#### Post date: [August 4, 2023, 11:45pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/4 "2023-08-04T23:45:11Z")

</div>

How would I go about implementing this? I’m using Wordpress with Memberpress, and just installed Discourse on a subdomain hosted on a digitalocean droplet. Do I literally upload the code as I would a plugin?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [August 7, 2023, 5:44am UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/5 "2023-08-07T05:44:02Z")

</div>

Yes, wp-discourse-mepr-sync is just a one-file Wordpress plugin.

[https://github.com/paviliondev/wp-discourse-mepr-sync](https://github.com/paviliondev/wp-discourse-mepr-sync)

---

<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 7, 2023, 7:35am UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/6 "2023-08-07T07:35:01Z")

</div>

> [@earlysound](#):
>
> How would I go about implementing this?

I just had a look at the code. In case it’s not clear, the Memberpress Sync plugin depends on code from the [WP Discourse](https://github.com/discourse/wp-discourse) plugin: [WP Discourse – WordPress plugin | WordPress.org](https://wordpress.org/plugins/wp-discourse/). That means you will also need to install, activate and configure the [WP Discourse](https://github.com/discourse/wp-discourse) plugin: [Connect WP Discourse to Discourse](https://meta.discourse.org/t/connect-wp-discourse-to-discourse/223490). You will also need to configure your WordPress site as the [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) provider for your Discourse site. Details about setting that up are here: [Configure single sign-on (SSO) with WP Discourse and DiscourseConnect](https://meta.discourse.org/t/wp-discourse-and-discourseconnect/223494).

There’s something in the Memberpress Sync plugin that I’m not sure about. It looks to me as though there are two Memberpress Product IDs and two Discourse groups hard coded into the plugin’s code:

> <https://github.com/paviliondev/wp-discourse-mepr-sync/blob/master/sync.php#L15-L17>

I _think_ you will need to make some changes to that file. The values in the array that’s defined by `PV_MEMBERPRESS_PRODUCT_IDS` will need to be set to your site’s subscription IDs. You will also need to set the `PV_DISCOURSE_ENROLLED_GROUP` and `PV_DISCOURSE_UNENROLLED_GROUP` definitions to groups that exist on your Discourse site.

It seems that the plugin adds any users who have an active subscription for any of your products to the `PV_DISCOURSE_ENROLLED_GROUP`. If a user who had one or more active subscriptions then has all their subscriptions expire, they will be removed from the `PV_DISCOURSE_ENROLLED_GROUP` and added to the `PV_DISCOURSE_UNENROLLED_GROUP`.

Maybe @fzngagan can confirm if this is correct? If it is, it will be fairly straightforward to setup the code for @earlysound’s site. It’s possible I’m misunderstanding how the plugin works though. I don’t have a copy of the Memberpress plugin. If I could get access to a development version of Memberpress, I could test it out myself.

---

<div class="post-metadata">

### Author: ![kendoer](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@kendoer](https://meta.discourse.org/u/kendoer)
#### Post date: [February 21, 2024, 8:49pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/8 "2024-02-21T20:49:21Z")

</div>

> [@simon](#):
>
> I _think_ you will need to make some changes to that file. The values in the array that’s defined by `PV_MEMBERPRESS_PRODUCT_IDS` will need to be set to your site’s subscription IDs. You will also need to set the `PV_DISCOURSE_ENROLLED_GROUP` and `PV_DISCOURSE_UNENROLLED_GROUP` definitions to groups that exist on your Discourse site.
> 
> It seems that the plugin adds any users who have an active subscription for any of your products to the `PV_DISCOURSE_ENROLLED_GROUP`. If a user who had one or more active subscriptions then has all their subscriptions expire, they will be removed from the `PV_DISCOURSE_ENROLLED_GROUP` and added to the `PV_DISCOURSE_UNENROLLED_GROUP`.
> 
> Maybe @fzngagan can confirm if this is correct? If it is, it will be fairly straightforward to setup the code for @earlysound’s site. It’s possible I’m misunderstanding how the plugin works though. I don’t have a copy of the Memberpress plugin. If I could get access to a development version of Memberpress, I could test it out myself.

I’m reading the same logic from the codes as you’ve explained. It would be really nice if @fzngagan could confirm.

---

<div class="post-metadata">

### Author: ![benm1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/benm1/32/196244_2.png) [@benm1](https://meta.discourse.org/u/benm1)
#### Post date: [October 11, 2024, 3:10pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/9 "2024-10-11T15:10:05Z")

</div>

Hey guys, I know it’s been a few months since the last post but hopefully the below is still helpful.

I’m not @fzngagan of course, but I am the client that this plugin was originally developed for!

The idea is that we wanted to automatically add users with an active recurring Memberpress membership to a specific Discourse group.

When users unsubscribed and left, they were removed from the Discourse group and added to an “Alumni” group instead, which had access to only certain areas of the forum.

@simon is correct in that the Membership IDs and Discourse groups are hard coded in. I believe it should just be a case of swapping them out for your own in order to use it.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [October 11, 2024, 3:14pm UTC](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447/10 "2024-10-11T15:14:30Z")

</div>

> [@benm1](#):
>
> is correct in that the Membership IDs and Discourse groups are hard coded in. I believe it should just be a case of swapping them out for your own in order to use it.

Yes, that’s right. You’d just need to change these values

```php
define('PV_MEMBERPRESS_PRODUCT_IDS', array(153, 161));
define('PV_DISCOURSE_ENROLLED_GROUP', 'locker');
define('PV_DISCOURSE_UNENROLLED_GROUP', 'KaizenAlumni');
define('PV_MEPR_ACTIVE_STATUSES', array('complete'));

```
