Memberpress: how to add users to groups upon Sign up


#1

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!


(Simon Cossar) #2

The easiest approach is to use the Discourse WordPress plugin: WP Discourse — WordPress Plugins 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/utilities.php at master · discourse/wp-discourse · GitHub.

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


#3

Thanks, Simon, will check those out!


#4

@Simon_Cossar

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


(Simon Cossar) #5

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.


#6

thanks, will give that a try!!


#7

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

(Simon Cossar) #8

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.


#9

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.


#10

@Simon_Cossar

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.


(Simon Cossar) #11

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


Bring over permission level from WordPress MemberPress
#12

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