Memberpress: how to add users to groups upon Sign up

Hey again Dani, I’m still on that one, worked on other stuff thinking it would help clearing up my mind but no luck.

In the My custom functions plugin, here’s the code I have :

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
$add_groups = array();
$remove_groups = array();

if ( current_user_can( 'mepr-active','memberships:1169' )  ) {
	$add_groups[] = 'Jeune';
} else {
	$remove_groups[] = 'Jeune';
}

if ( current_user_can( 'mepr-active','memberships:1170' )  ) {
	$add_groups[] = 'Parent';
} else {
	$remove_groups[] = 'Parent';
}

$params['add_groups'] = join( ',', $add_groups );
$params['remove_groups'] = join( ',', $remove_groups );

return $params;
}

As you can see I only have 2 memberships, Teen and Parent.

Did you ONLY put your code in My custom functions and it worked or you did something else elsewhere and I missed it ? I usually am decent in resolving these issues but this one is really bugging me and I don’t understand why…

On discourse, the user never gets added to any group, it’s just SSO-friendly and his email is activated, but I can’t seem to make the membership communicate with the group

I’m super sorry to abuse of your time like that but this is starting to make me really pissed off so I want it cleared out of the way asap

Did you add any webhook ? Did anything in the WP-Discourse plugin or the Discourse parameters thing ?

Hi Eva, I went over your code and there’s no errors that I can spot - and I totally get your frustration. If it hadn’t been for Simon Cossar I don’t know how I’d have resolved it myself.

So, taking it backwards: if the PHP is correct, then something else must be disconnected. (browniepoints for me, yay.)

But…
I don’t know enough about any of that to be able to give suggestions or help.
If you’ve got your forum parked with the boys from Discourse Hosting, you could ask Richard (his name tag won’t pop, but this is his profile: Profile - RGJ - Discourse Meta )

Or perhaps @simon might be able to provide you with the missing steps.

Keeping my fingers crossed for you.

edit:
p.s. these are all the related plug-ins i use (+ that PHP one)
(Memberpress Drip is email marketing, so not related to this)

3 Likes

At least the code is okay…

Just going to ask some easy questions just to be sure it’s not a stupid mistake :

On discourse :

  • Are your groups hidden/closed/opened ?
  • Is your group full name different from the name and default title ? Which one is considered by the PHP code ?
  • Have you done anything special in the parameters section concerning this ?

On wordpress :

  • Does your WP-Discourse plugin have specific settings/webhooks for this ?
  • Do you validate the user’s email when they register on wordpress or you use Discourse’s email validation ?

I’m not sure the issue is there but I always take a shot, thank you again !

On Discourse:

  • groups are any and all: closed and opened (i don’t give access to hidden groups, come to think of it)
  • these are some of the group names as displayed in ‘Groups’ - and as you can see, the “NoSpacesNames” are the ones that coincide with the PHP settings
    26
    54
  • If by parameters you mean the following, then no:
    22

nor in the actual group settings (where you create it, with name, owner, etc.), it’s all straight forward and not connected to accessing through SSO - so setting or not setting this as a person’s primary group is not relavant.

There is one webhook here on Discourse that I put in as instructed by the WP-Discourse plugin:


but I don’t actually have that box checked…

  • email verification technically goes via memberpress: they (my wp-instance) send the log-in details to the user. by clicking the log-in link the users verify their address.
    i know for a fact that it’s not through Discourse, because for a while it ALSO went through discourse (glitch somewhere) and Richard sorted that for me.

you’re welcome!

2 Likes

With this method, is it possible to assign members as trust level 2 in Discourse when they open an account by paying, and to drop it to trust level 1 when they do not pay monthly?

I haven’t looked at it for a while, but what you would need is a communication between your Discourse codes and your Payment codes (in my case: memberpress).

So if the person stops paying, memberpress changes their code, and this code corresponds to a different group level.
If there’s no link with the payment then the group level will not be adjusted.

Oh. Wait.
Re-reading your question: i’m not sure how it works with trust levels…
but… if all else fails, you can easily create a GROUP that is set to trust level 2 and another that is set to trust level 1 and have your membership plug in assign those groups to your members.

hope that helps. if not, i’ll have to dive back in. it’s been up and running since the previous conversation and i’ve never looked at it since.

1 Like

That is the great idea! Yes, we can set trust levels to Discourse groups. But i am not experienced with Memberpress enough.

Can we get paying users in Memberpress to a special group automatically?

This came up in another post on Meta today, so I had a look at the code to confirm whether or not a user’s trust level can be reduced by adding them to a group that sets a lower trust level than the user’s current group. From looking at the code, I can see that this is not possible:

  def grant
    if @user.trust_level < @trust_level
      @user.change_trust_level!(@trust_level)
      @user.save!
    end
  end

If a user’s trust level is lower than the trust level that is granted by the group, adding the user to the group will update their trust level. Otherwise, there will be no change in the user’s trust level.

1 Like

There’s probably other membership plug-ins who can do the same thing for you.

it’s 8pm this end and i’m just signing off for the day, but if you need more help i’d be happy to look into it in the morning. just let me know.

1 Like

i’m a little rusty (also, it’s late) but the trust level wouldn’t really be relevant. i don’t use trust levels at all. Group TL 2 has access to ‘this’ and Group TL1 does not.
the actual trust level is not in use. any member can be trust level 1 and still gain access (through payment) to Group TL 4 - provided I do not set permission to trust levels, but only to groups.

1 Like

Yes, the way to approach this is to use groups and category security permissions. There shouldn’t be a need to rely on trust levels.

2 Likes

@Dani1, thanks for sharing your trials and tribulations here, it’s very helpful for others who use MemberPress like myself.

That does make sense, but I’m curious what happens if the member’s subscription expires and they never logout so they continue to access the group… Have you found this to be an issue?

If so, I’m wondering if there is a way to force a refresh or logout…

Thanks!
Ray

1 Like

You need to catch the Memberpress event and adjust the groups accordingly.

See also Bring over permission level from WordPress MemberPress and Managing Discourse group membership with WP Discourse SSO

4 Likes

@RGJ The code above marked as the solution does remove members from groups, but as Dani1 mentioned, she said it doesn’t kick in until they log off.

If a user is updated through the normal SSO login process the update will not occur until they logout and log back in again. The code example above is the easiest way to approach the problem, but it’s probably not the best way to manage group membership.

The WP Discourse plugin has a couple of helper functions that are enabled when WordPress functions as the SSO provider site for Discourse. These functions allow you to update group memberships without requiring the user to logout of Discourse. Those functions are in the topic that Richard linked to: Managing Discourse group membership with WP Discourse SSO.

The post that Richard linked to gives some details about the two different approaches. I’m going to add those details to the #howto topic next week. For now it would be good to read through the post.

2 Likes

Ah, I think I understand. Part of my confusion is in the Bring over permission level from WordPress MemberPress thread, near the bottom it pointed to this thread as the solution so it got confusing.

It’s still over my feeble head, but I look forward to your #howto on the subject.

Thanks to all for your help!
Ray

1 Like

Then log them off :slight_smile:
Something like this should do it:

  $url = sprintf('https://%s/users/by-external/%d.json', $discourseHostname, $wpUserID);
  $options = [
    'headers' => [
      'Api-Key' => $apiKey,
      'Api-Username' => 'system'
    ]
  ];
  $request = wp_remote_get($url, $options);
  if (wp_remote_retrieve_response_code($request) != 200) {
	return; // user not found, bail out
  }
  
  $userData = json_decode(wp_remote_retrieve_body($request), true);
  if (!isset($userData['user']['id'])) {
	return false; // incorrect response, bail out
  }
  
  $discourseUserID = $userData['user']['id'];
  
  $url = sprintf('https://%s/admin/users/%d/log_out', $discourseHostname, $discourseUserID);
  $options = [
    'headers' => [
      'Api-Key' => $apiKey,
      'Api-Username' => 'system'
    ]
  ];
  $request = wp_remote_post($url, options);
1 Like

you can log them out on their behalf. just impersonate them.

in fact, just going to their user-details should let you log them out.

(obviously, that’s all very manual. i’ve only got a handful of peeps and know exactly what everyone is up to. Haven’t tried Richard’s solution yet, but good to know it’s there!)

could you please add a link here when that happens? Thanks!

1 Like

Sure, the topic is here: Managing Discourse group membership with WP Discourse SSO. It describes how to use the WP Discourse add_user_to_discourse_group and remove_user_from_discourse_group functions. Assuming that you have your WordPress site configured to be the SSO provider site for Discourse, those are the functions that you should use to manage group memberships on Discourse.

The example that topic uses is with the PaidMembershipsPro plugin, but a similar approach should work with any well made WordPress membership plugin.

I’m going to add some details to the topic about how to manage group membership with the add_groups and remove_groups SSO parameters. For most cases, managing group membership by adding those parameters to the SSO payload will not be the best approach, because it requires users to logout and log back into Discourse before their group membership will be updated.

My hope is that in the near future there will be an out-of-the-box solution for linking Discourse with specific WordPress membership plugins. Currently it can only be done by adding a small amount of custom code to your WordPress site. If this isn’t something that you normally do, you may need to hire a developer to help with this.

4 Likes

Thanks for the details. I’ll see if I can squeeze them into my head.

I agree – I’d rather have groups addition/subtraction done on the fly rather than upon user logout then login.

If I can’t get it sorted, I’ll hire a developer here to do it and share the code. I think there are a couple who have done it in the marketplace section.

Ray