Managing Discourse group membership with WP Discourse SSO

Hello everyone, I come for help, I’m a little stuck with this group management

I get that the code is incomplete but it should be enough to add users into the Discourse groups, I wanted to test it a little bit before adding other hooks if needed. So far, nothing is syncing, here is my config (sorry, it’s in french in the screenshots)

Paid Membership Pro


Groups in Discourse

So, id1 in PMPro should sync with Discourse group “gratuit”, id2 with “abonnes” and id3 with “entreprises”

Part of the code that I needed to edit (the rest don’t need any change if i’m correct)

// Allows you to call the wp-discourse functions without having to write the namespace each time you use them.
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

// Returns the Discourse group name that is associated with a membership id. With
// PaidMembershipsPro, you can see the membership id on the Membership Levels page. With other
// membership plugins, you may need to call a function to get access to a `membership_id/membership_name` array.
function dcpmp_get_level_for_id( $id ) {
    $levels_to_discourse_groups = array(
        1 => 'gratuit',
        2 => 'abonnes',
        3 => 'entreprises'
    );

(...) 

I didn’t do any mistake I guess

I have a few (dumb) questions to be sure :

  1. I have a edit the functions.php file located in wp-includes\ and insert the code in the end of the file, right ?

  2. For the discourse groups, is it the name (all lowercase) or the Full Name we have to use in the functions.php ?
    I’ll use an example, for the id2 <=> abonnes, here is the Discourse group config :

image
I’m supposed to use abonnes instead of Abonnés, right ?

  1. Do I need to do something specific to trigger the syncing ? I tried to create a new member in the paid membership pro, logout/login to Discourse, without success

  2. Do I need to configure a webhook in the WP DIscourse plugin ? I was thinking of Update Userdata, but when I tried it, nothing happened

I tried to create two news groups, walking and running to follow the example, I didn’t work either so, it probably comes from one stupid mistake on my part. If anyone have a clue, I’ll take it.

Anyway, the wordpress plugin is really impressive, even with little knowledge on wordpress it’s really easy to configure :clap:

No, the file you need to edit is the functions.php file that is in the theme that is activated on your site.

Another way to do it is to create a small WordPress plugin with the code. That way it doesn’t depend on your theme.

You get the group name from the Name field, not the Full Name field.

The syncing is triggered by this part of the code:

if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
    add_action( 'pmpro_added_order', 'dcpmp_add_member_to_group' );
    add_action( 'pmpro_after_change_membership_level', 'dcpmp_remove_member_from_group', 10, 3 );
}

No, this doesn’t depend on webhooks.

The code that’s supplied in this topic is intended to be a starting point for developers. If you’re not familiar with WordPress development, you may need to hire someone to do the work for you. If you’re going to do the work yourself, I would recommend setting up a local WordPress development environment, so that you can work on the code without risking breaking your website.

5 Likes

Thanks for the complete answers, I may be able to succeed with these

Hi,

We would like to integrate our LearnDash (which have several courses) with Discourse Forum (with subdomain).

So when someone purchase and have access to a Learndash course (for example Course A), it will automatically get access to Forum Course A in our Forum.

We already setup Group Permission to Access Specific Category, as we setup each course have one forum categories.

So the next challenge is, how to make integration so when a person purchase course A in Learndash, will automatically granted Discourse Group Permission course A.

Based on explanation in this forum, that every membership plugin will call functions when add or remove access level, we ask Learndash about it.

Here are their answers (we are not so sure, if it is what we are looking for) :slight_smile:


You can use the following to enroll a user into a course:

ld_update_course_access( $user_id, $course_id, $remove );

The function take two required parameters, $user_id and $course_id. The third parameter will default if set to true will remove the user from the course.

And the following action is for WHEN a user enrolls into a course:

/**
 * Run actions after a users list of courses is updated
 * 
 * @since 2.1.0
 * 
 * @param  int  	$user_id 		
 * @param  int  	$course_id
 * @param  array  	$access_list
 * @param  bool  	$remove
 */
do_action( 'learndash_update_course_access', $user_id, $course_id, $access_list, $remove );

// Some comments here
public String getFoo()
{
    return foo;
}

Let me know if that helps!


Is the answer above are the functions that we need to integrate with Discourse?

If yes, how to integrate Discourse Function and Learndash function here?

Thanks a lot.

Does anyone here know of a more OOTB solution that could be used to sync WP user groups/permissions to Discourse groups and permissions?

I’ve read a handful of posts from @simon where he talks about a plugin that he created at one point, but is no longer being supported. I’m at the stage where I’m researching which WP tool I want to use to manage memberships, but I want to consider which would be the easiest to integrate with Discourse to create this seamless permission sync between my WP and Discourse sites.

See WP Discourse plugin installation and setup. It will allow you to use your WordPress as SSO master (or the other way around).

It’s well supported and works well.

3 Likes

As far as WordPress membership platforms go, the only one I’m aware of that has built in Discourse integration is Memberful: https://memberful.com/

More specific documentation of their Discourse integration: Integrate Discourse with Memberful

It costs a bit more than some other WP membership solutions (like Woo, Memberpress, etc.) all in since they take a % transaction fee as well. But it does seem pretty user friendly and full-featured.

Haven’t used it personally (we use Woo) but this is prob what I’d use if I wanted to not get into too much custom technical stuff. Though worth noting the above functions Simon has made to manage groups from WP > Discourse are relatively easy to use; if you wanted to use a different membership platform a professional WP dev could most likely get the basic integration set up in half a day or less.

2 Likes

The others are not too hard, but they do take a bit of cushion code.

3 Likes

Cushion code, eh?

image

4 Likes

Err, custom code? Might be required?

Nope. You said cushion code.

1 Like

Sounds like the kind of thing a custom title would be perfect to immortalise :laughing:

2 Likes

That’s true. However, I am unable to decipher the code in your cushion.

does this start the sync automaticly ? Or do we need to do something ?

is there someone who is willing to help me with this ?

I believe it adjusts group membership whenever someone logs in again. Have you set it up?

Yes, i have everything set-up.

i have a connection between discourse and WP
image

i have Paid membership pro set-up and the woocommerce settings are good.
When i make a test order there is a membership.

Now the sync for the users won’t sync, and the memberships also

1 Like

does anyone nows if this still works ? Or canm anyone help me ?

The add_user_to_discourse_group and remove_user_from_discourse_group functions should still be working correctly. The easiest way to test this is to call the functions directly from the WP CLI shell.

The code that is required for syncing group memberships with Discourse is fairly complex. I wouldn’t attempt to write the code directly on a live WordPress site. You should setup a WordPress development site on your local computer for writing the code, or hire someone to do the work for you. Writing the code doesn’t require much knowledge of Discourse. Any WordPress developer who is familiar with how your membership plugin works should be able to handle it. If they get stuck, we can try to help them here.

2 Likes

i get this from discourse when i logged in at the webhooks request:

{
  "user": {
    "id": 744,
    "username": "webwinkeladviseur",
    "name": "Patrick Heijmans",
    "avatar_template": "/user_avatar/forum.xxxx.nl/webwinkeladviseur/{size}/5511_2.png",
    "email": "xxxx@xxxx.nl",
    "last_posted_at": null,
    "last_seen_at": "2019-02-18T07:41:05.723Z",
    "created_at": "2019-02-18T07:25:29.746Z",
    "stats": [
      {
        "action_type": 13,
        "count": 1
      }
    ],
    "trust_level": 0,
    "moderator": false,
    "admin": false,
    "title": null,
    "badge_count": 0,
    "pending_count": 0,
    "profile_view_count": 0,
    "time_read": 0,
    "recent_time_read": 0,
    "primary_group_name": null,
    "primary_group_flair_url": null,
    "primary_group_flair_bg_color": null,
    "primary_group_flair_color": null,
    "staged": false,
    "second_factor_enabled": false,
    "post_count": 0,
    "locale": null,
    "muted_category_ids": [],
    "watched_tags": [],
    "watching_first_post_tags": [],
    "tracked_tags": [],
    "muted_tags": [],
    "tracked_category_ids": [],
    "watched_category_ids": [],
    "watched_first_post_category_ids": [],
    "private_messages_stats": {
      "all": 1,
      "mine": 0,
      "unread": 1
    },
    "system_avatar_template": "/letter_avatar_proxy/v2/letter/w/7ea924/{size}.png",
    "muted_usernames": [],
    "mailing_list_posts_per_day": 49,
    "external_id": "143",
    "invited_by": null,
    "groups": [
      {
        "id": 10,
        "automatic": true,
        "name": "vertrouwensniveau_0",
        "display_name": "vertrouwensniveau_0",
        "user_count": 154,
        "mentionable_level": 0,
        "messageable_level": 0,
        "visibility_level": 0,
        "automatic_membership_email_domains": null,
        "automatic_membership_retroactive": false,
        "primary_group": false,
        "title": null,
        "grant_trust_level": null,
        "incoming_email": null,
        "has_messages": false,
        "flair_url": null,
        "flair_bg_color": null,
        "flair_color": null,
        "bio_raw": null,
        "bio_cooked": null,
        "public_admission": false,
        "public_exit": false,
        "allow_membership_requests": false,
        "full_name": null,
        "default_notification_level": 3,
        "membership_request_template": null
      }
    ],
    "featured_user_badge_ids": [],
    "user_option": {
      "user_id": 744,
      "email_always": false,
      "mailing_list_mode": false,
      "mailing_list_mode_frequency": 1,
      "email_digests": true,
      "email_private_messages": true,
      "email_direct": true,
      "external_links_in_new_tab": true,
      "dynamic_favicon": false,
      "enable_quoting": true,
      "disable_jump_reply": false,
      "digest_after_minutes": 10080,
      "automatically_unpin_topics": true,
      "auto_track_topics_after_msecs": 240000,
      "notification_level_when_replying": 2,
      "new_topic_duration_minutes": 2880,
      "email_previous_replies": 2,
      "email_in_reply_to": true,
      "like_notification_frequency": 1,
      "include_tl0_in_digests": false,
      "theme_ids": [
        32
      ],
      "theme_key_seq": 0,
      "allow_private_messages": true,
      "homepage_id": null,
      "hide_profile_and_presence": false,
      "text_size": "normal",
      "text_size_seq": 0
    }
  }
}