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/
- The Discourse site: https://jamcircle.secretsofsolo.com/
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.