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

**URL:** https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777
**Category:** WordPress
**Created:** [July 4, 2020, 2:10pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777 "2020-07-04T14:10:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![alexjpanagis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexjpanagis/32/263387_2.png) [@alexjpanagis](https://meta.discourse.org/u/alexjpanagis)
#### Post date: [July 4, 2020, 2:10pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/1 "2020-07-04T14:10:25Z")

</div>

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 🙂

Take care & talk soon,

Alex

---

<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 4, 2020, 4:38pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/2 "2020-07-04T16:38:06Z")

</div>

I’m assuming you are using the [WP Discourse](https://github.com/discourse/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 [Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse-sso/74724) 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.

---

<div class="post-metadata">

### Author: ![alexjpanagis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexjpanagis/32/263387_2.png) [@alexjpanagis](https://meta.discourse.org/u/alexjpanagis)
#### Post date: [July 4, 2020, 4:51pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/3 "2020-07-04T16:51:46Z")

</div>

Hey Simon – yes, that is correct I’m using the [WP Discourse](https://github.com/discourse/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!

---

<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 4, 2020, 5:14pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/4 "2020-07-04T17:14:19Z")

</div>

> [@alexjpanagis](#):
>
> as long as it’s a general example that can add every new member to a group

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](https://github.com/discourse/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.):

```php
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:

```php
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.

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

    return $params;
}

```

or

```php
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.

---

<div class="post-metadata">

### Author: ![alexjpanagis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexjpanagis/32/263387_2.png) [@alexjpanagis](https://meta.discourse.org/u/alexjpanagis)
#### Post date: [July 6, 2020, 1:52pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/5 "2020-07-06T13:52:19Z")

</div>

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

---

<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 6, 2020, 5:26pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/6 "2020-07-06T17:26:55Z")

</div>

> [@alexjpanagis](#):
>
> Out of interest, would it be too difficult to get an example for WooCommerce Memberships in there?

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.

---

<div class="post-metadata">

### Author: ![alexjpanagis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexjpanagis/32/263387_2.png) [@alexjpanagis](https://meta.discourse.org/u/alexjpanagis)
#### Post date: [July 6, 2020, 6:44pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/7 "2020-07-06T18:44:48Z")

</div>

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?

---

<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 6, 2020, 9:52pm UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/8 "2020-07-06T21:52:35Z")

</div>

> [@alexjpanagis](#):
>
> 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](https://github.com/discourse/wp-discourse) plugin. Developers can feel free to message me if they get stuck on anything.

---

<div class="post-metadata">

### Author: ![alexjpanagis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexjpanagis/32/263387_2.png) [@alexjpanagis](https://meta.discourse.org/u/alexjpanagis)
#### Post date: [July 7, 2020, 1:04am UTC](https://meta.discourse.org/t/automatically-adding-new-users-from-wp-integration-to-a-group/156777/9 "2020-07-07T01:04:33Z")

</div>

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](https://github.com/discourse/wp-discourse) plugin let’s see. 🙂
