Manage group membership in Discourse with WP Discourse SSO

Yeah. The error message is fine (I guess it should mention “sso master”, but I knew right away). It’s just that it wasn’t what I expected, and it’s taken me forever to figure it out. (Not your fault.)

What I’m trying to do now is have a function called at login. I’m trying to get the Discourse username so that I can then do an old-school curl to an API call to add to the group.

		$user_meta= get_user_meta( $user_id, 'discourse_sso_user_id', true );

doesn’t seem to be finding the user. Is that not how it works?

That is the correct way to get the user’s Discourse id if it has been set. $user_id needs to be the WordPress user’s ID. If you’re getting an empty response, the user has not logged in via Discourse yet. Try making the call with a user you know has logged into WordPress from Discourse.

1 Like

Sorry. It turns out that the problem is me not knowing how to get debugging info out of php/wordpress.

I now have the discourse_user_id, so now I can construct a curl to add the user to the desired group. I hope.

Thanks for your help.

For debugging WordPress, see this post: https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse/74724/51.

1 Like

Yeah, writing to a log file is what I wanted to do, but it seems that the only way I have access to the log on this webhost is by getting it emailed to me. :frowning:

My solution is pushing stuff to https://pipedream.com/ . It’s quite painful, but better than anything else I’ve found today. My problem before is that I need to do something different to push an array to the pipedream webhook than I do for the integer that get_user_meta returned.

1 Like

Well, it seems that if I do a curl within a wp_login action, it interferes with the SSO connection. (OOps, I’m not passing the API key, but I don’t think that’s the problem.)

add_action( 'wp_login', 'wc_login_manage_group', 10, 2);

Is there maybe something that gets called immediately after SSO is complete?

Maybe

wpdc_sso_client_after_login_link

edit: I think that might do it. Now to see if I can add to group via user_id rather than username. . .

nope. Need to figure out how php will let me access user_nicename out of $user = wp_get_current_user(); It’s right there.

Well, of course, it’s $discourse_username = $user->data->user_nicename;

I don’t think that php had that -> syntax when I learned it in 1997. :slight_smile:

But now, the user_nicename changes. Sometimes it’s correct, and sometimes it’s got a -2 appended. I’m logging out and then clicking Login via your forum account in the same browser window. This doesn’t seem right.

It goes back and forth between these:

  • “usernames”: “pfaffman_delete_gmai”
  • “usernames”: “pfaffman_delete_gmai-2”
1 Like

You’re kind of in uncharted territory with this. It’s going to be hard to develop it on a live site. I’m not sure what’s going on with the nice_name, but I’m going to take a closer look at that tomorrow.

4 Likes

Yeah. That’s on me. Setting up a staging sites with SSO seemed like it’d be a bigger problem than fighting with what was already working. It was going to be simple. :wink: This is one more reason to finish my Ansible tooling to support WordPress. :wink:

I’m a Discourse Sysadmin, dammit, not a WordPress developer!

Thanks. Unlike pretty much every other question I’ve had in this exchange, that does seem like a :bug:. Thanks for all of your help!

1 Like

Yes, this is a bug. I can only reproduce it if a username is changed on Discourse after an account is created on WordPress. I’ll get this fixed by early next week.

3 Likes

Holy :cow:! How’d you manage to find it!? Nice work.

By adding log statments to my development site. This should be an easy fix, but I’d rather not push the change to WordPress before I test it out some more.

3 Likes

Sure you have a reasonable development environment. Like a real developer. That I understand (in spite of my not having such!) But even thinking to try usernames that had changed… I guess the 2 was the clue.

Here’s a slightly different method to handle Paid Memberships Pro group sync. Pop this into your functions.php file. Expected behavior is add upon subscribe and remove upon cancellation. Thanks to @dfriestedt for sponsoring.

https://github.com/justindirose/dc-pmp-group-sync

8 Likes

Awesome, anything stopping this being wrapped into a plugin for customers who aren’t confident editing PHP?

1 Like

No, probably not, but PHP/WP development isn’t my forte, and I don’t plan on personally actively maintaining this code long-term.

3 Likes

I am implementing the PMPro code above and am hung up on something. My understanding is that this code only fires when users buy subscription or cancel a subscription.

Is there a way that I can auto-sync users as they are created on Discourse? In my case, I have some users who are subscribers but don’t have a Discourse accounts (will be created via SSO).

If wordpress is the SSO master then you can have it update groups at every login.

1 Like

Is there a code snippet that shows an example of updating groups on WP login?

The add_user_to_discourse_groups function will create a Discourse user if it doesn’t already exist. If that function is being called for your users, then you should be fine. Where you might run into problems will be for existing WordPress users who got their membership before the code was added to your site. If that is the case, you will probably need to add the users to Discourse groups through the SSO parameters that are passed to Discourse on login.

To pass additional SSO parameters with the WP Discourse plugin, you can hook into the 'wpdc_sso_params' filter. The parameter you need to use to add users to a Discourse group is called add_groups. You can use it with something like this:

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if ( /* some condition that returns true if groups should be added for the user */ ) {
        $params['add_groups'] = 'comma,separated,group,names'; // Don't use spaces between names.
    }

    return $params;
}
7 Likes

I realise this is only sample code but it appears to have a pretty critical bug.

If the payment isn’t completed and the status of the membership is “token”

The group is still added to the discourse user :open_mouth:

I’ll look in to this at a later date but i think it’s worth mentioning


1 Like