Add user to group via webhook with email

Good day,

I am trying to add users from Wordpress to private groups in Discourse via webhook.

I use WP Discourse plugin as a SSO login method and I don’t allow users to change their username in Discourse. I made this because I thought that this way I could use the username from Wordpress to add them to a group in Discourse. Because, I thought they would be the same.

The problem is that when the username is generated automatically in Wordpress, sometimes uses their email as their username. But Discourse doesn’t allow this in usernames, so it breaks the username at “@”.

Example:
Wordpress email: jhon@gmail.com
Wordpress username (generated automatically): jhon@gmail.com
Discourse username (generated through WP Discourse SSO): jhon

This is not allowing me to use the wordpress user to add them to a group.

Is there an easier way to make this?

It would be awesome to be able to add users to a group with their email instead of username. The email is always available in any app (CRM, Wordpress, etc), so it won’t be needed the extra step to make a “GET” request to the API to get the username from that email and then PUT them in the group.

I use Fluent Forms to send this data to the webhook from Wordpress when they send a form, so I can’t make the GET request and then PUT the username.

Thank you!

1 Like

If I’m understanding you correctly, you’re doing this

  1. Using DiscourseConnect with Wordpress as the provider and Discourse as the client.
  2. Using a Webhook in Wordpress to put users in groups in Discourse (.e. PUT to /g/:group_name/members (api ref)) as a result of a form action.

if that’s right you can find the Discourse username in Wordpress in the discourse_username user meta field, so

get_user_meta($user_id, 'discourse_username', true);

Use that to get the username to use in the PUT request following the form action.

You can ensure that discourse_username is filled for your users using the user webhooks in the WP Discourse plugin (note we’re talking about different webhooks from the ones you’re currently using in your form).

2 Likes