# Wordpress SSO - Restrict Content / user levels / groups

**URL:** https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776
**Category:** WordPress
**Created:** [February 22, 2017, 10:53am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776 "2017-02-22T10:53:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tomobrien](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomobrien/32/67930_2.png) [@tomobrien](https://meta.discourse.org/u/tomobrien)
#### Post date: [February 22, 2017, 10:53am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/1 "2017-02-22T10:53:56Z")

</div>

I have [WP discourse](https://github.com/discourse/wp-discourse) working for SSO and Restrict Content Pro for subscription, signup, but I am wondering how to set up levels during signup … That carry over to discourse?

Eg reader level 1, contributor level 2, moderator, level 3

So Signup through Wordpress, and level affects group in Discourse?

---

<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: [February 22, 2017, 11:01am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/2 "2017-02-22T11:01:13Z")

</div>

Are you committed to using Restricted Content Pro? There are quite a few different WordPress membership plugins. My plan is to make a general wp-discourse-groups plugin that will work for most of them, but at the moment I’m making a plugin specifically for the WishList Member plugin. When it’s finished, I think it will do everything that you are looking for.

---

<div class="post-metadata">

### Author: ![tomobrien](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomobrien/32/67930_2.png) [@tomobrien](https://meta.discourse.org/u/tomobrien)
#### Post date: [February 22, 2017, 11:16am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/3 "2017-02-22T11:16:13Z")

</div>

Thats great! I am opening to using any of them. Keep us posted! : )

---

<div class="post-metadata">

### Author: ![tomobrien](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomobrien/32/67930_2.png) [@tomobrien](https://meta.discourse.org/u/tomobrien)
#### Post date: [February 22, 2017, 12:46pm UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/4 "2017-02-22T12:46:37Z")

</div>

Is there no way to do this with current [WP Discourse](https://github.com/discourse/wp-discourse) setup?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 8, 2017, 4:25pm UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/5 "2017-03-08T16:25:28Z")

</div>

I’ve written code for some WordPress subscription manager that used API calls to manage groups on WordPress. It shouldn’t be hard to set up WordPress groups that whatever group manager you uses could connect to.

---

<div class="post-metadata">

### Author: ![deere](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@deere](https://meta.discourse.org/u/deere)
#### Post date: [July 23, 2020, 10:57am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/6 "2020-07-23T10:57:11Z")

</div>

Hi guys, resurrecting this old post, because my question applies to _Restrict Content Pro_, so is similar to the OP. Basically, I’m helping with a site where the membership system is Restrict Content Pro and I’m trying to integrate Discourse group sync for paid RCP memberships- basically anyone with any membership needs to sync to the `Members` group on their Discourse forum.

I’ve spent a lot of time trying to get something like Simon’s _Managing Discourse group membership with [WP Discourse](https://github.com/discourse/wp-discourse) SSO_ example up and running. I have SSO working fine, but memberships still aren’t _syncing_ (add members to a group on successful membership activation, or remove members from a group on expiration of membership).

For this post I’m just trying to get adding new members to a Discourse group working. The site’s Discourse forums only have one Discourse group – `Members`. (Removing groups should be easy enough as the _Restrict Content Pro_ hook is the same (just replacing the `active` suffix with `expired`). Restrict Content Pro lists the [rcp\_transition\_membership\_status\_active](https://docs.restrictcontentpro.com/article/2130-rcp-transition-membership-status) action hook as triggering whenever a membership’s status changes. It has parameters for `$old_status` and `$membership_id`. Running [rcp\_get\_membership()](https://docs.restrictcontentpro.com/article/2080-rcp-get-membership) returns an `RCP_Membership` object (which contains a reference to the Wordpress $user\_id among other things).

Here’s the most simplified version I’ve come up with:

```php

use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

// Adds a user to the 'members' Discourse group.
function rcpdc_add_member_to_group( $membership_id ) {
    $membership = rcp_get_membership( $membership_id );
    $user_id = $membership->get_user_id();
    DiscourseUtilities::add_user_to_discourse_group( $user_id, 'Members' );
}

// Make sure to check that the Discourse class exists.
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
    // rcp action suffixed with {active} runs whenever a new membership is added
    add_action( 'rcp_transition_membership_status_active', 'rcpdc_add_member_to_group' );
}

```

If you can see something I’m obviously getting wrong that would stop this action from running properly, I’d really appreciate the help. Thanks!

---

<div class="post-metadata">

### Author: ![deere](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@deere](https://meta.discourse.org/u/deere)
#### Post date: [July 23, 2020, 11:44am UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/7 "2020-07-23T11:44:03Z")

</div>

Oops! I had an error on the Restrict Content Pro side of things, sorry. The action hook I’m using requires two arguments-

```php
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

// Adds a user to the 'members' Discourse group.
function rcpdc_add_member_to_group( $old_status, $membership_id ) {
    $membership = rcp_get_membership( $membership_id );
    $customer = $membership->get_customer();
    $user_id = $customer->get_user_id();
    DiscourseUtilities::add_user_to_discourse_group( $user_id, 'Members' );
}

// Make sure to check that the Discourse class exists. If not, and you deactivate wp-discourse, this will crash your site.
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
    add_action( 'rcp_transition_membership_status_active', 'rcpdc_add_member_to_group', 10, 2 );
}

```

With the above change, the add\_action should be working now, but I’m still not having any success with the add\_user\_to\_discourse\_group function.

Newly activated memberships still aren’t being added to the `Members` group successfully, so I would still love it if anyone could offer me any advice. Cheers.

---

<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: [July 23, 2020, 5:02pm UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/8 "2020-07-23T17:02:02Z")

</div>

> [@deere](#):
>
> I’m still not having any success with the add\_user\_to\_discourse\_group function.

If possible, try creating a `debug.log` file on your WordPress server and then writing to that file from your `rcpdc_add_member_to_group` function. You can use that to make sure that the function is getting called and for checking that the correct values are being assigned to the variables you are creating.

For debugging, I usually use the approach that is described here: [Debugging in WordPress – Advanced Administration Handbook | Developer.WordPress.org](https://wordpress.org/support/article/debugging-in-wordpress/#wp_debug_log). You need to manually create the `debug.log` file and add it to your `wp-content` directory.

---

<div class="post-metadata">

### Author: ![deere](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@deere](https://meta.discourse.org/u/deere)
#### Post date: [July 24, 2020, 1:19pm UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/9 "2020-07-24T13:19:30Z")

</div>

> [@simon](#):
>
> try creating a `debug.log` file on your WordPress server and then writing to that file from your `rcpdc_add_member_to_group` function.

Thanks, Simon! Worked a treat. I got the code working. Turns out the code wrapped around the action was preventing the add\_action from running the function. Once I removed the

```php
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {

```

and just included the add\_action itself, the plugin worked fine. I’m not really worried about the check, because I’m the only person administering this site. So yeah, now syncing works perfectly for both adding and removing members from groups.

Thanks again for your fast and helpful reply.

---

<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: [July 24, 2020, 11:28pm UTC](https://meta.discourse.org/t/wordpress-sso-restrict-content-user-levels-groups/57776/10 "2020-07-24T23:28:42Z")

</div>

Thanks for looking into that. I add an option to the plugin for dealing with comments that are in private categories. Unfortunately I ran out of time to get that done this week. The option will be added to the next update of the plugin. I’ll try to get that done early next week.
