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!