Memberpress: how to add users to groups upon Sign up

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 http://commonmark.org/help/tutorial/09-code.html

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 ?

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