Memberpress + WP Discourse (Groups Management)

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:

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

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 function described here

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):

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:

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

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.

6 Likes

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 ?

4 Likes

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

1 Like

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?

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

1 Like

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 plugin: WP Discourse – WordPress plugin | WordPress.org. That means you will also need to install, activate and configure the WP Discourse plugin: Connect WP Discourse to Discourse. You will also need to configure your WordPress site as the DiscourseConnect provider for your Discourse site. Details about setting that up are here: Configure single sign-on (SSO) with WP Discourse and DiscourseConnect.

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:

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.

1 Like

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

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.

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

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'));
1 Like