# Llevar nivel de permisos desde WordPress MemberPress

**URL:** https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074
**Category:** WordPress
**Created:** [15 Enero, 2018 16:26 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074 "2018-01-15T16:26:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [15 Enero, 2018 16:26 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/1 "2018-01-15T16:26:13Z")

</div>

We are currently using MemberPress for our membership platform on WordPress. We are going to be launching a ‘free’ membership level on WordPress and would like the ability to have this membership correspond to 1-2 discussions in our Discourse forum (but have the rest of the forum content protected from free members).

Is there a way to bring over membership roles/permissions into Discourse using the SSO plugin?  
@simon you helped us with a few things with the [WP Discourse](https://github.com/discourse/wp-discourse) plugin in the past. If the above is not possible out of the box, is it something you’d be able to build on a contract basis? If so, maybe we can discuss the details offline.

---

<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: [15 Enero, 2018 16:56 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/2 "2018-01-15T16:56:16Z")

</div>

There are a couple of ways to do it. The easiest way is to add the Discourse group names to the SSO payload. You can do this by hooking into the `wpdc_sso_params` filter. Setting it up this way will add and remove users from groups when they login to Discourse. You’ll need to figure out the condition to check the user against to know which groups to add or remove them from. Memberpress fires an action when a membership level is added or removed. You could hook into that action and add some user metadata to a user when they are added or removed from a group. You could then test for that with the function below.

```php
add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if ( $user /* some condition based on the user to add groups */ ) {
        $params['add_groups'] = 'comma,separated,group,names'; // Don't use spaces between names.
    }

	if ( $user /* some condition based on the user to remove groups */ ) {
		$params['remove_groups'] = 'comma,separated,group,names'; // Don't use spaces between names.
	}

    return $params;
}

```

The other approach is to use the wp-discourse `add_user_to_discourse_group` and `remove_user_from_discourse_group` functions. This way users can be added and removed from Discourse groups without them first having to login to Discourse. There is some information about using the functions here: [Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse/74724).

I’m working for Discourse now, so I can’t take on any contract work. Setting this up is fairly straightforward. If you find someone to do the work, and they get stuck, have them post on here and we can sort it out.

---

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [16 Enero, 2018 21:00 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/3 "2018-01-16T21:00:41Z")

</div>

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.
    } else {
      $params['remove_groups'] = 'Academy_Free'; // Don't use spaces between names.
    }

    return $params;
}

```

`current_user_can('mepr-active','memberships:12856)` is a function in MemberPress that checks if a member is active in a specific membership. Does that look right? If so, that code runs every time someone connects to Discourse via SSO, correct? If this looks right, I may wait until we push the free membership live and then test this functionality—unless you have a way we could test it now with a WP staging site.

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: [16 Enero, 2018 22:13 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/4 "2018-01-16T22:13:36Z")

</div>

> [@apautler](#):
>
> unless you have a way we could test it now with a WP staging site

The code looks right to me. It’s tricky to test it with a staging site. Unless you add some code so that the user’s external ids can be guaranteed to be unique between your staging and your production sites it can mess up your SSO records on Discourse.

---

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [18 Enero, 2018 17:01 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/5 "2018-01-18T17:01:23Z")

</div>

Just pushed things live and this code works as expected. Thank you!

---

<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: [28 Marzo, 2018 18:07 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/6 "2018-03-28T18:07:20Z")

</div>

> [@apautler](#):
>
> current\_user\_can(‘mepr-active’,'memberships:12856) is a function in MemberPress that checks if a member is active in a specific membership.

@simon

Simon, if i want to use this for ALL my membership levels, would it be enough to just say “current\_user\_can(‘mepr-active’)” or would i have to include all the active ID’s?

There’s five groups and i need the user to enter the group he has paid for.  
If they change plans, that should be reflected in their group.

Based on what Andrew used, i have this:  
&nbsp;

 ![44](https://global.discourse-cdn.com/meta/original/3X/3/6/3623c61c093d756c70413f6b3d23560e9466f4a7.png)

does that make sense for my situation?

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: [28 Marzo, 2018 18:24 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/7 "2018-03-28T18:24:51Z")

</div>

The basic idea makes sense. `current_user_can` is a WordPress function. Its documentation is [here](https://codex.wordpress.org/Function_Reference/current_user_can). I think that in this case you are fine to call it without the `$object_id` argument. If MemberPress adds `mepr-active` as a `role` or `capability` to users when they sign up for any membership level, and removes the `mepr-active` `role` or `capability` when the membership expires, then it will do what you are looking for.

Be careful with the comment (`// Don't use spaces between names.`). Comments in that form need to be included on a single line, or they will cause an error. You can remove the comment from the code.

---

<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: [28 Marzo, 2018 18:26 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/8 "2018-03-28T18:26:32Z")

</div>

awesome, thanks so much, Simon!

---

<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: [28 Marzo, 2018 18:31 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/9 "2018-03-28T18:31:11Z")

</div>

It is a lot more difficult to set up this kind of code in a live environment than it is in a development environment, because if you make a mistake on the live site, you can break the site. Make sure that you have some way of accessing your site through the backend (using cPanel or the command line.) If you make a mistake you’ll be able to fix it that way.

---

<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: [28 Marzo, 2018 18:34 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/10 "2018-03-28T18:34:30Z")

</div>

thanks for the heads up.  
if anything does go wrong, is it enough to just remove the code i added? or will i really be up S-creek?

i’m using the plugin “My Custom Functions”

> **[My Custom Functions](https://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 …

---

<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: [28 Marzo, 2018 18:40 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/11 "2018-03-28T18:40:31Z")

</div>

Thanks for the link! If it works as it says it does, then that is a good approach. The worst case scenario is that if you added some code that broke your site, you would need to edit the code through the file manager in your site’s cPanel.

---

<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: [28 Marzo, 2018 18:42 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/12 "2018-03-28T18:42:33Z")

</div>

Thanks!

So far, so good, everything’s still working, i ran a test account just now, but then i remembered i read somewhere there’s a 10 minute lag, so i’ll try again in a couple of minutes.

---

<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: [28 Marzo, 2018 20:43 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/13 "2018-03-28T20:43:07Z")

</div>

@simon

well, the site didn’t break… but it also didn’t work… ☹

it’s close to midnight on this end, so i’m calling it a day. will try again in the morning.

any suggestions will be much appreciated!

thanks!

Edit: i can try adding the membership ID’s. what would be the way to add Numerous ID’s?  
Like so: current\_user\_can(‘mepr-active’,‘memberships:12856,12345,12346’) ?

---

<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: [29 Marzo, 2018 10:27 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/14 "2018-03-29T10:27:47Z")

</div>

i came up with this:

```
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:47281,47295,47299,47303,47297,47301,47305,48259,48238’) ) {
$params['add_groups'] = 'Forever_Free,Creative_License,Join_Live,Transform,Transform_Plus';     
} else {
$params['remove_groups'] = 'Forever_Free,Creative_License,Join_Live,Transform,Transform_Plus'; } 
return $params;
}

```

but the plug-in tells me “This causes a fatal error so it hasn’t been applied.”  
i tried with and without spaces: ‘memberships:47281, 47295’ but both options are rejected.

---

<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: [29 Marzo, 2018 15:23 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/15 "2018-03-29T15:23:40Z")

</div>

If the code you used is exactly the same as what you posted here, the problem might be the curly quotes in your code. Try this code. I’ve also removed the `$user` parameter in the function call, since it’s not used:

```php
add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
	if ( current_user_can( 'mepr-active','memberships:47281,47295,47299,47303,47297,47301,47305,48259,48238' ) ) {
        $params['add_groups'] = 'Forever_Free,Creative_License,Join_Live,Transform,Transform_Plus';
    } else {
        $params['remove_groups'] = 'Forever_Free,Creative_License,Join_Live,Transform,Transform_Plus';
	}

	return $params;
}

```

---

<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: [31 Marzo, 2018 12:30 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/16 "2018-03-31T12:30:52Z")

</div>

@simon

Thanks, Simon, indeed, my text editor gives curly quotes, 😕

unfortunately the code didn’t help. but… not giving up, 🙂

EDIT! It DID work, i had made an error in matching the names between Groups and WP users.  
**YEEHA!!! couldn’t be happier!!**

BUT… they now get added to ALL groups.  
so i will have to add the php one-by-one, each to their corresponding group.

like: 47281 leads to “Forever Free”  
the next two lead to “Creative License” etc.

RE-EDIT:  
I’ve inserted the code through my plugin in various ways. (i tried making a separate entrance for every group, but clearly you can only add one filter and the rest should follow within that one filter)

some of my attempts were directly bounced, so i discarded those.  
the code below has been accepted, but it’s not working properly.

the first group works as it should. but then the next don’t, so i’m making a mistake in the code.

RE-EDIT: Turns out i had missed the curly quotes again…  
**Problem resolved,**  **the below code works.** (once you take out the curlies 😀😀😀 )

```
   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,47299' ) ) {
        $params['add_groups'] = ‘CreativeLicense’;
    } else {
        $params['remove_groups'] = ‘CreativeLicense’;
	}
    if ( current_user_can( 'mepr-active','memberships:47303,47297' ) ) {
        $params['add_groups'] = ‘JoinLive’;
    } else {
        $params['remove_groups'] = ‘JoinLive’;
	}
if ( current_user_can( 'mepr-active','memberships:47301,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;
}

```

thanks for your help!  
You have NO idea how happy i am. Just … awesome!

:- D.

POST-EDIT:  
to anyone wanting to use this code for their own memberpress/discourse connection: it’s not complete yet!  
when a member upgrades or downgrades, they get added to the new group, but they don’t get removed from the old group. still working on that.

Final Edit:  
Solution can be found here: [Memberpress: how to add users to groups upon Sign up - #11 by simon](https://meta.discourse.org/t/memberpress-how-to-add-users-to-groups-upon-sign-up/83478/11)

---

<div class="post-metadata">

### Author: ![cacao](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@cacao](https://meta.discourse.org/u/cacao)
#### Post date: [9 Abril, 2018 17:21 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/17 "2018-04-09T17:21:20Z")

</div>

Dani1,

We are also Memberpress users. Is your site in production and if so can you share a link?

thx!

---

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [8 Junio, 2018 15:34 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/18 "2018-06-08T15:34:16Z")

</div>

@simon Coming back to this because we need to have multiple groups added/removed and it seems to only be apply the last group. Essentially I changed the code to check for multiple different memberships (similar to Dani1’s code above). It works except when someone is a member of multiple memberships. In that situation it only applies the last group, which makes sense since it is looking for the latest value of $params. Is there a way to have multiple checks without having to nest conditions, which could start to get pretty messy?

---

<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: [9 Junio, 2018 18:26 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/19 "2018-06-09T18:26:10Z")

</div>

> [@apautler](#):
>
> It works except when someone is a member of multiple memberships. In that situation it only applies the last group, which makes sense since it is looking for the latest value of $params.

You could try creating arrays for the groups you wish to add and remove. For each condition you check, add to the arrays. Something like the code below could be used inside of your function.

```plaintext
//...

$groups_to_add = [];
$groups_to_remove = [];

if ( /* some condition */ ) {
    $groups_to_add[] = 'group_one'; // $array[] = 'element' adds 'element' to the array.
    } else {
    $groups_to_remove[] = 'group_one';
}

if ( /* another condition */ ) {
    $groups_to_add[] = 'group_two';
    } else {
    $groups_to_remove[] = 'group_two';
}

//...

$params['add_groups'] = join( ',', $groups_to_add ); // Create a string from the array, with ',' as the separator.
$params['remove_groups'] = join( ',', $groups_to_remove );

return $params;

```

Make sure you are calling `join()` with no spaces in the separator parameter (`','`).

---

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [13 Junio, 2018 20:25 UTC](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074/20 "2018-06-13T20:25:25Z")

</div>

Great idea Simon. That worked perfectly. Thank you!

[Página siguiente](https://meta.discourse.org/t/bring-over-permission-level-from-wordpress-memberpress/78074.md?page=2)
