# Automatically create Discourse user on Wordpress registration

**URL:** https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107
**Category:** WordPress
**Tags:** discourseconnect
**Created:** [January 14, 2025, 5:35pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107 "2025-01-14T17:35:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![37Rb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/37rb/32/224158_2.png) [@37Rb](https://meta.discourse.org/u/37Rb)
#### Post date: [January 14, 2025, 5:35pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/1 "2025-01-14T17:35:14Z")

</div>

My site uses [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Provider so Wordpress manages logins to Discourse.

When a user registers in Wordpress, I would like a Discourse user to be immediately created for them and the Discourse activation email sent to them. But that doesn’t seem to happen.

I have checked the “Sync user data” bit in Wordpress settings.

 ![Screenshot 2025-01-14 at 11.26.48 AM](https://global.discourse-cdn.com/meta/original/4X/1/b/f/1bf946966b224b84871c7ae511fda96584b04018.png)

It seems like the description says it will create a Discourse user when they log into to Wordpress. But I don’t see that happening. The discourse user isn’t created until they visit the Discourse site.

Is my integration not working properly? Or am I misunderstanding this description?

Thanks!

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [January 15, 2025, 2:00pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/2 "2025-01-15T14:00:27Z")

</div>

Hey there @37Rb, this is what’s happening.

https://www.loom.com/embed/9e41fce76a124380b1efde71caf10263?sid=8a534176-81f9-42e4-8265-9b2ccc9005b3

Let me know if you have any further questions after watching the video.

---

<div class="post-metadata">

### Author: ![37Rb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/37rb/32/224158_2.png) [@37Rb](https://meta.discourse.org/u/37Rb)
#### Post date: [January 15, 2025, 2:43pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/3 "2025-01-15T14:43:31Z")

</div>

I understand now. It is creating a new Discourse user when they “log in” to Wordpress. It’s not creating a new user when they “register” on Wordpress. Our users can register themselves and on registration they are immediately considered logged in. So at that point they are logged in but don’t have a Discourse user.

If they log out of WP and log back in to WP then a Discourse user is created. I just verified that.

We don’t require emails to be verified in Wordpress because it’s a WooCommerce site and it automatically registers them if they make a purchase while not logged in. The first time they visit Discourse after being registered they are getting the address verification email from Discourse.

The problem users are the ones that both:

- Register in WP but don’t log out and log back in
- Never visit Discourse (which is our support site for the ecommerce site)

Wordpress/WooCommerce treats self-registration as a login - they are considered logged in immediately after they register. Is there any way to make it so a Discourse user is created when they self-register and not just when they log out and log back in?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [January 15, 2025, 3:03pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/4 "2025-01-15T15:03:04Z")

</div>

Hey Ryan, you’re right that the plugin is creating a user after login. Specifically, the plugin is using [wp\_login](https://developer.wordpress.org/reference/hooks/wp_login/) hook. There is also a [user\_register](https://developer.wordpress.org/reference/hooks/user_register/) hook in Wordpress, however the reason the plugin doesn’t use that is because this fires immediately after a record is inserted into the Wordpress database, regardless of whether their details (in particular their email) is verified.

> [@37Rb](#):
>
> WooCommerce treats self-registration as a login

To be slightly cheeky, I think the question here might be why doesn’t WooCommerce trigger the `wp_login` hook if that is the case 🙂 But, more helpfully, there is likely some other hook or filter that WooCommerce does fire immediately after both registration and login, if that is indeed what it’s doing.

> [@37Rb](#):
>
> Is there any way to make it so a Discourse user is created when they self-register and not just when they log out and log back in?

The reason why we haven’t built this into the plugin (as yet), is because, as you might see from your case, there are many different ways users can be created/registered/logged in in Wordpress.

But lets see if we can get your specific case working. First, we need to find a hook that fires immediately after your registration/auto-login is happening. Can you confirm exactly how you have that setup? i.e. Exactly what flow you’re using. The more specific the better, e.g. any documentation you used.

---

<div class="post-metadata">

### Author: ![37Rb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/37rb/32/224158_2.png) [@37Rb](https://meta.discourse.org/u/37Rb)
#### Post date: [January 15, 2025, 5:35pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/5 "2025-01-15T17:35:17Z")

</div>

I suspect that WooCommerce is what’s causing the confusion. It looks like automatically being logged in after registration is a WooCommerce feature, not a Wordpress feature. And they intentionally don’t fire the login actions.

[https://github.com/woocommerce/woocommerce/issues/22905](https://github.com/woocommerce/woocommerce/issues/22905)

I can search for an appropriate WooCommerce hook to use for this. Is there a [WP Discourse](https://github.com/discourse/wp-discourse) function I can call or hook I can fire to create the Discourse user if I find the right WooCommerce hook?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [January 15, 2025, 6:50pm UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/6 "2025-01-15T18:50:16Z")

</div>

Yes, so once you find the right hook to use, you can create the user on Discourse like so:

```php
$user = get_user_by( 'id', $user_id );
$params = WPDiscourse\Utilities\Utilities::get_sso_params( $user );
WPDiscourse\Utilities\Utilities::sync_sso_record( $params, $wp_user_id )

```

I’m assuming you’ll have the `user_id` of the new user in this context.

---

<div class="post-metadata">

### Author: ![37Rb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/37rb/32/224158_2.png) [@37Rb](https://meta.discourse.org/u/37Rb)
#### Post date: [January 16, 2025, 2:13am UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/7 "2025-01-16T02:13:02Z")

</div>

The woocommerce\_created\_customer action seems to work well.

In addition to the code above, this is also useful to mark the email address as verified.

```php
delete_user_meta( $user_id, 'discourse_email_not_verified' );

```

It’s working so far. Thanks for the help!!!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [February 15, 2025, 2:13am UTC](https://meta.discourse.org/t/automatically-create-discourse-user-on-wordpress-registration/347107/8 "2025-02-15T02:13:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
