# 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:** 1\
**Showing post:** 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.

---

_[View the full topic](https://meta.discourse.org/t/memberpress-wp-discourse-groups-management/238447)._
