Wordpress SSO - Restrict Content / user levels / groups

I have 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?

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.

4 Likes

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

Is there no way to do this with current WP Discourse setup?

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.

1 Like

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 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 action hook as triggering whenever a membership’s status changes. It has parameters for $old_status and $membership_id. Running 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:


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!

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

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.

1 Like

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 | WordPress.org. You need to manually create the debug.log file and add it to your wp-content directory.

1 Like

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

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.

1 Like

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.

2 Likes