Memberpress: how to add users to groups upon Sign up

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!

1 Like

The easiest approach is to use the Discourse WordPress plugin: WP Discourse – WordPress plugin | WordPress.org 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 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: wp-discourse/lib/utilities.php at main ¡ discourse/wp-discourse ¡ GitHub.

There is some information about using these functions in this topic: Manage group membership in Discourse with WP Discourse SSO.

3 Likes

Thanks, Simon, will check those out!

@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:

Would appreciate some further insight, thank you!

Dani

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.

1 Like

thanks, will give that a try!!

In the parallel post on this topic (Bring over permission level from WordPress MemberPress) 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!

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;
}

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:

$remove_groups = array();

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

	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']

$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.

5 Likes

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

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

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.

@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.

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.

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;
}

Use backticks to create codeblocks to get syntax highlighting. See Markdown Tutorial - Code

7 Likes

YES!! IT WORKS!!! :heart_eyes: :heart_eyes: :heart_eyes: :heart_eyes:

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

 
 
 

thankyou4

4 Likes

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

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

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’.

1 Like

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

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!

1 Like

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 ?

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

1 Like

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

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 ?