Automatically Adding New Users (from WP integration) To A Group

I apologize if this has been asked before but I am looking for a way to automatically add users which were added via the WP integration (SSO) to be added to a certain user group.

I was unable to find any documentation as to how to accomplish this so any guidance would be appreciated :slight_smile:

Take care & talk soon,

Alex

2 Likes

I’m assuming you are using the WP Discourse plugin for SSO. That plugin has a couple of functions that can be used for managing Discourse group membership. Have a look at Managing Discourse group membership with WP Discourse SSO for details about how to use the functions. I’m going to update that topic soon to add a more general example . Let me know if anything in the topic is unclear.

3 Likes

Hey Simon – yes, that is correct I’m using the WP Discourse plugin. Thanks for linking to this topic, I’ll keep an eye out for the update but if it’s any help the specific case is using WooCommerce Memberships & Subscriptions. That being said, I intend on limiting user registrations to paying members only anyway, so as long as it’s a general example that can add every new member to a group I have called everybody (just for context, the goal here is just to make messaging & tagging members in announcements altogether easy).

Take care & talk soon!

1 Like

Yes, that’s the example that is missing from the topic I linked to. You can add users to a group as a part of the SSO login process by using the add_groups SSO parameter. By default, the WP DIscourse plugin doesn’t send this parameter with the SSO payload, but the plugin has a filter that can be used to add this parameter to the SSO payload.

The following code, added to your theme’s functions.php file, or to a plugin, should work for you. You can add users to multiple groups in this way. The add_groups parameter accepts a comma separate list of group names (with no spaces before or after the commas.):

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
    $params['add_groups'] = 'your_group_name'; 

    return $params;
}

If you only wanted to add specific users to the group, you could call the function like this:

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if (/*add a condition here to check if the user should be added to the group */) {        
        $params['add_groups'] = 'your_group_name'; 
    }

    return $params;
}

You can also remove users from groups with the remove_groups SSO parameter.

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
    $params['remove_groups'] = 'your_group_name'; 

    return $params;
}

or

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if (/*add a condition here to check if the user should be removed from the group */) {        
        $params['remove_groups'] = 'your_group_name'; 
    }

    return $params;
}

The main downside to this approach compared to using the add_user_to_discourse_group function that I linked to is that it requires existing users to log out and then log back into Discourse before their group memberships will be updated.

3 Likes

Awesome, thank you Simon! Out of interest, would it be too difficult to get an example for WooCommerce Memberships in there?

It would really help if it would automatically remove people from the group as well when there is no longer an active WooCommerce subscription and only add them to the group when there is – that way no manual review is necessary and everything is completely automated (when plans are cancelled & activated again etc.). Because the permissions in Discourse can be limited to only let people view or post when they have an active subscription (and are in the group that this adds them to)…

Any guidance that I’d be able to pass onto a developer to get this added would be super helpful.

Take care,

Alex

It should be possible to automatically remove users from a Discourse group when their WooCommerce membership has expired. To do that, you would use the remove_user_from_discourse_group function that I linked to.

I am not familiar enough with the WooCommerce Memberships & Subscriptions plugin to know what action hooks are called when a membership is created or cancelled. Possibly some other members of the Meta community will know to do it. You could also create a topic in our #marketplace category to find a developer to help with this.

2 Likes

Alright great, thanks for this Simon. I’ll look into it and see if I can hire a developer with experience with Discourse (and will pass this onto them). Speaking of which – out of interest, do you have a list of trusted experts you can recommend for this type of work?

I don’t have a list of members of this forum who do WordPress/Discourse integrations. I would be interested to know which developers in the Meta community are interested in working with WordPress/Discourse.

I haven’t received any negative reports about community members related to WordPress integrations. If you hire a developer who has a history in the community, you should be safe. I’m always happy to answer questions related to building integrations on top of the WP Discourse plugin. Developers can feel free to message me if they get stuck on anything.

2 Likes

Great, I suppose I’ll be in touch at some point then! Might end up bundling all of these changes into a little add-on/extension for the WP Discourse plugin let’s see. :slight_smile: