# Memberpress：如何在注册时将用户添加到组

**URL:** <https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478>\
**Category:** WordPress\
**Created:** [2018年三月21日 13:48 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478 "2018-03-21T13:48:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年三月21日 13:48 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/1 "2018-03-21T13:48:54Z")

</div>

Hi, I use Memberpress on my WP site.

My Discourse forum is set to user groups, rather than permissions levels. Being a member of a group is what determines permissions.

At this moment new members land perfectly inside Discourse, but they come in at Trust\_0 and then I have to manually assign them to their group.

How can I connect the membership levels to the Discourse Groups so that this happens automatically upon Signup?

Thanks!

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2018年三月21日 16:00 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/2 "2018-03-21T16:00:34Z")

</div>

> [@Dani1](#):
>
> I use Memberpress on my WP site.

> [@Dani1](#):
>
> How can I connect the membership levels to the Discourse Groups so that this happens automatically upon Signup?

The easiest approach is to use the Discourse WordPress plugin: [WP Discourse – WordPress plugin | WordPress.org](https://wordpress.org/plugins/wp-discourse/) and enable Single Sign On between WordPress and your forum. This will make your WordPress site responsible for all user authentication on Discourse.

To automatically add users to groups will require a bit of custom code. The [WP Discourse](https://github.com/discourse/wp-discourse) plugin has a couple of functions that can be used when SSO is enabled for automatically adding and removing users from Discourse groups. The functions are `add_user_to_discourse_group` and `remove_user_from_discourse_group`. You can find them here: [https://github.com/discourse/wp-discourse/blob/master/lib/utilities.php#L309](https://github.com/discourse/wp-discourse/blob/master/lib/utilities.php#L309).

There is some information about using these functions in this topic: [Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse/74724).

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年三月21日 16:07 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/3 "2018-03-21T16:07:16Z")

</div>

Thanks, Simon, will check those out!

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年三月28日 16:00 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/4 "2018-03-28T16:00:30Z")

</div>

@simon

Hi Simon, I have been looking into the various bits and pieces of info provided but can’t figure out how to connect the dots.

I’ve created the corresponding group names as user roles in WP, but I don’t know what it means to “add the Discourse group names to the SSO payload. You can do this by hooking into the `wpdc_sso_params` filter,” ---- like you mentioned in @apautler Andrew’s post.

Further down in that post, Andrew mentions he found ‘the right code’ - which seems to be what i need as well… but i have no idea where to stick it:

> [@Bring over permission level from WordPress MemberPress](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/3):
>
> Thanks for the quick response @simon. I think I am going to try to go with the first option. Because the free membership isn’t setup on the live WordPress site yet, I am having a hard time testing this. Here is the code that I have now: add\_filter( 'wpdc\_sso\_params', 'wpdc\_custom\_sso\_params', 10, 2 ); function wpdc\_custom\_sso\_params( $params, $user ) { if ( current\_user\_can('mepr-active','memberships:12856') ) { $params['add\_groups'] = 'Academy\_Free'; // Don't use spaces between names…

Would appreciate some further insight, thank you!

Dani

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2018年三月28日 16:24 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/5 "2018-03-28T16:24:27Z")

</div>

> [@Dani1](#):
>
> Further down in that post, Andrew mentions he found ‘the right code’ - which seems to be what i need as well… but i have no idea where to stick it:

You could either add that code directly to your theme’s `functions.php` file, or you could make a small plugin that contains the code. Adding it to the `functions.php` file is easier, but adding to to a plugin is safer.

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年三月28日 16:26 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/6 "2018-03-28T16:26:07Z")

</div>

thanks, will give that a try!!

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年四月3日 09:56 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/7 "2018-04-03T09:56:49Z")

</div>

In the parallel post on this topic ([Bring over permission level from WordPress MemberPress - #16 by Dani1](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/16)) I came to the following code (this one corrected for curly quotes).

The below code adds people to their corresponding Discourse group when signing up through Memberpress.

BUT.

when they up- or downgrade, it adds them to the new group, but it does NOT remove them from their previous group.

@simon  
Simon, any idea how to adjust for that?

Thanks!

```plaintext
add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
	if ( current_user_can( 'mepr-active','memberships:47281' ) ) {
		$params['add_groups'] = 'ForeverFree';
	} else {
		$params['remove_groups'] = 'ForeverFree';
	}

	if ( current_user_can( 'mepr-active','memberships:47295,47297' ) ) {
		$params['add_groups'] = 'CreativeLicense';
	} else {
		$params['remove_groups'] = 'CreativeLicense';
	}

	if ( current_user_can( 'mepr-active','memberships:47299,47301' ) ) {
		$params['add_groups'] = 'JoinLive';
	} else {
		$params['remove_groups'] = 'JoinLive';
	}

	if ( current_user_can( 'mepr-active','memberships:47303,47305' ) ) {
		$params['add_groups'] = 'Transform';
	} else {
		$params['remove_groups'] = 'Transform';
	}

	if ( current_user_can( 'mepr-active','memberships:48259,48238' ) ) {
		$params['add_groups'] = 'TransformPlus';
	} else {
		$params['remove_groups'] = 'TransformPlus';
	}

	return $params;
}

```

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2018年四月3日 15:49 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/8 "2018-04-03T15:49:29Z")

</div>

I formatted your code so that it’s easier to see what’s going on.

`$params['remove_groups]` keeps getting redefined. As an example, if a user had been initially added to the ‘ForeverFree’ group on WordPress, and had then been removed from it, when the function is called for that user `$params['remove_groups']` will first get set to ‘ForeverFree’, but in the next `if` statement it will get set to ‘CreativeLicense’… By the end of the function `$params['remove_groups']` will probably have been set to ‘TransformPlus’.

There are a few ways you could approach this. We can’t get too into helping with PHP code on this forum, but you could try creating a `$remove_groups` variable that is an array:

```plaintext
$remove_groups = array();

```

and then in each `if` statment, add the group name to `$remove_groups` array:

```plaintext
	if ( current_user_can( 'mepr-active','memberships:47281' ) ) {
		$params['add_groups'] = 'ForeverFree';
	} else {
		$remove_groups[] = 'ForeverFree'; // Adds the groupname to the array.
	}

	if ( current_user_can( 'mepr-active','memberships:47295,47297' ) ) {
		$params['add_groups'] = 'CreativeLicense';
	} else {
		$remove_groups[] = 'CreativeLicense';
	}

    // ...

```

At the end of the function, join the `$remove_groups` array with a comma and assign it to `$params['remove_groups']`

```plaintext
$params['remove_groups'] = join( ',', $remove_groups ); // Converts the array into a comma separated string.

```

If it’s possible for users to be _added_ to more than one group at a time, you might need to take a similar approach with `$params['add_groups']`.

If you’re testing this with an admin user, I think that `current_user_can( 'mepr-active', ... )` will always return `true` for an admin user. It’s something to watch out for.

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年四月3日 17:00 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/9 "2018-04-03T17:00:04Z")

</div>

That makes sense. Thank you, Simon! am going to try that.

> [@simon](#):
>
> If you’re testing this with an admin user, I think that current\_user\_can( ‘mepr-active’, … ) will always return true for an admin user. It’s something to watch out for.

no, i’ve been testing with ‘newly created users and test-accounts’, testing my payment gateways while i’m at it.  
&nbsp;

> [@simon](#):
>
> If it’s possible for users to be added to more than one group at a time, you might need to take a similar approach with $params[‘add\_groups’].

No, they should only be in one group at once. it’s not a biggie if they’re in lower-level groups, because they would have those permissions anyway, but for all-round convenience it would be just the one group, which gets ‘more inclusive’ as the level progresses.

which probably means that if ‘array’ doesn’t help, i could also try inverting the order of the memberships. if forever free comes last rather than first, it doesn’t matter if users don’t get taken out of that group.

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年四月3日 19:21 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/10 "2018-04-03T19:21:33Z")

</div>

@simon

Simon, if i understood you correctly, this is what it should now look like.  
(i’d have used colours - except i don’t know how…)

but… I may not have completely understood what to do with the final parameters.  
i first included them ‘once, at the end’, then, when that didn’t work, i included them ‘after every statement’.

the plug-in accepts the code as valid, but users do not get removed from the old group, so in functionality this one remains the same as what i started with.

```
 add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
    function wpdc_custom_sso_params( $params ) {
        if ( current_user_can( 'mepr-active','memberships:48259,48238' ) ) {
            $params['add_groups'] = 'TransformPlus';
        } else {
            $remove_groups[] = 'TransformPlus';
    	} {
        $params['remove_groups'] = join( 'ForeverFree','CreativeLicense','JoinLive','Transform','TransformPlus', $remove_groups );
        }
        if ( current_user_can( 'mepr-active','memberships:47303,47305' ) ) {
            $params['add_groups'] = 'Transform';
        } else {
            $remove_groups[] = 'Transform';
        } {
        $params['remove_groups'] = join( 'ForeverFree','CreativeLicense','JoinLive','Transform','TransformPlus', $remove_groups );
        }
        if ( current_user_can( 'mepr-active','memberships:47299,47301' ) ) {
            $params['add_groups'] = 'JoinLive';
        } else {
            $remove_groups[] = 'JoinLive';
    	} {
        $params['remove_groups'] = join( 'ForeverFree','CreativeLicense','JoinLive','Transform','TransformPlus', $remove_groups );
        }
        if ( current_user_can( 'mepr-active','memberships:47295,47297' ) ) {
            $params['add_groups'] = 'CreativeLicense';
        } else {
            $remove_groups[] = 'CreativeLicense';
    	} {
        $params['remove_groups'] = join( 'ForeverFree','CreativeLicense','JoinLive','Transform','TransformPlus', $remove_groups );
        }
        if ( current_user_can( 'mepr-active','memberships:47281' ) ) {
            $params['add_groups'] = 'ForeverFree';
        } else {
            $remove_groups[] = 'ForeverFree';
        } {
        $params['remove_groups'] = join( 'ForeverFree','CreativeLicense','JoinLive','Transform','TransformPlus', $remove_groups );
        }
            
            return $params;
    }

```

i understand that you can’t go too deeply into this matter, but you’re my last resort.  
i’ve asked everywhere else, including memberpress and the developers they recommend.

i don’t speak php, at all. everything i’ve done so far is extrapolate from the snippets of code i’ve found left and right - particularly the pieces you’ve shared with me.

as it is right now, i’m ‘happy’, because people get put into the correct groups. but they don’t get taken out of the old one, and so that’s more steps for me to keep track of – so if there’s any way you might provide the missing code, that would just be… awesome…

thanks!

p.s., inverting the order did not help.

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2018年四月3日 19:39 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/11 "2018-04-03T19:39:10Z")

</div>

Try this. I haven’t tested it, so there could be errors. It’s worth having this as a reference. Let me know if it doesn’t work.

```plaintext
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:48259,48238' ) ) {
		$add_groups[] = 'TransformPlus';
	} else {
		$remove_groups[] = 'TransformPlus';
	}

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

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

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

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

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

	return $params;
}

```

> [@Dani1](#):
>
> i’d have used colours - except i don’t know how…

Use backticks to create codeblocks to get syntax highlighting. See [Markdown Tutorial - Code](http://commonmark.org/help/tutorial/09-code.html)

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年四月3日 20:14 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/12 "2018-04-03T20:14:56Z")

</div>

YES!! IT WORKS!!! 😍 😍 😍 😍

the user has to log out and back in, which makes sense, and then he’s gone from the old group.

&nbsp;  
&nbsp;  
&nbsp;

 ![thankyou4](https://global.discourse-cdn.com/meta/original/3X/5/8/58d9c9bb90241e32e6066fd5e42ae106f773bd16.png)

---

<div class="post-metadata">

**Author:** ![Evapy](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Evapy](https://meta.discourse.org/u/Evapy)\
**Post date:** [2018年六月23日 19:02 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/13 "2018-06-23T19:02:20Z")

</div>

Hey again Simon, excuse my lack of knowledge but where exactly do you put this code ? Do I have to create a function or a class in a specific file in a specific plugin ?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年六月23日 20:04 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/14 "2018-06-23T20:04:11Z")

</div>

> [@Evapy](#):
>
> Do I have to create a function or a class in a specific file in a specific plugin ?

Hi Evapy, you can use a plugin for this (in WP):

> **[My Custom Functions](https://nl.wordpress.org/plugins/my-custom-functions/)**
>
> Easily and safely add your custom PHP code to your WordPress website, directly out of the WordPress Admin Area, without the need to have an external e …

I use the free version. Works perfect. The good part is: if you don’t have the right code, it won’t install that code, so it won’t mess anything up. It will tell you so in a message. Do take care of the quotes, though. No curly quotes, only ‘straight ones’.

---

<div class="post-metadata">

**Author:** ![Evapy](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Evapy](https://meta.discourse.org/u/Evapy)\
**Post date:** [2018年六月23日 20:33 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/15 "2018-06-23T20:33:13Z")

</div>

Hey Dani, thanks that’s the perfect plugin

But just wondering, were you able to do that when an user gets let’s say X membership, it automatically puts him in the X group also ?

I created the group “X” on memberpress and said that the downgrade path was membership “X”, but I think that the user doesn’t get automatically put in this group, only in the membership…

Is that clear ? lol

I just want to be sure on how to automatically put an user in a specific group when he uses a specific membership

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年六月24日 19:15 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/16 "2018-06-24T19:15:24Z")

</div>

i haven’t looked at it since setting it up - it works, so i’ve had no need - and a lot of it has since dropped out of my memory, but one of the things i remember from when i set it up, is that i hadn’t noticed how the groups had similar names in Memberpress and Discourse - but not IDENTICAL!  
so **_i_** knew they were the same group, but my php-code didn’t.

**the memberpress name for the group isn’t really relevant** : it’s the Memberpress ID (the numerical code) that takes care of that. so you want to make sure that group X is the name of your group inside DISCOURSE.

hope that helps!

---

<div class="post-metadata">

**Author:** ![Evapy](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Evapy](https://meta.discourse.org/u/Evapy)\
**Post date:** [2018年六月24日 19:16 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/17 "2018-06-24T19:16:51Z")

</div>

Oh yes I get it, my problem is that the users aren’t automatically added in a group in Memberpress, just in a membership…

Were you able to link a membership with a group ?

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年六月24日 19:17 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/18 "2018-06-24T19:17:41Z")

</div>

yes. the code i used a few posts up is exactly the code i’m using.

so memberships:number takes care of the Memberpress end  
and ‘ForeverFree’ is the name of the discourse group

---

<div class="post-metadata">

**Author:** ![Dani1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dani1/32/84172_2.png) [@Dani1](https://meta.discourse.org/u/Dani1)\
**Post date:** [2018年六月24日 19:18 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/19 "2018-06-24T19:18:14Z")

</div>

obviously, you do need to have installed whatever it is that makes memberpress and discourse talk… the SSO end…

---

<div class="post-metadata">

**Author:** ![Evapy](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Evapy](https://meta.discourse.org/u/Evapy)\
**Post date:** [2018年七月2日 17:30 UTC](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/20 "2018-07-02T17:30:36Z")

</div>

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 ?

[下一頁](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478.md?page=2)
