Unable to create user in Discourse from WP with new registration form

I have Discourse Connect setup with WP as the main register and login instance.

I have two user registration forms with different flows

  1. Standard registration from Homepage (this is the old one and still creates a user in Discourse)
  2. User registration by using a tool flow. (this one creates the WP user but does not create a user in Discourse).

I don’t see any settings in Discourse plugin that are specific to a registration form. When I go to the user, I don’t see a Discourse username in WP for the second form. Link

I have two questions -

  1. What could I be missing here? Any additional setup requires to make it work for a new form?
  2. How can I create a user in Discourse and connect it to WordPress for members that exist in WP?

Correction: On further analysis, it seems like some users are not created in discourse even from the first form.

1 Like

Has anyone faced this problem? I am really stuck here.

Hey @Himanshu_Singh,

Could you walk me through, step by step in terms of how a user sees it:

  1. What happens with your “standard registration” form;
  2. What happens with your “user registration” form.

e.g.

  1. A user goes to the form and enters their details
  2. A user gets redirected to discourse…

Include the steps where something you don’t want to happen happens and steps you’d like to happen instead.

2 Likes

Hi Angus,

In standard registration,

  1. The user goes to the form and adds their name, email and password
  2. Then the user is automatically logged in
  3. Then when the user clicks on community link, they are automatically logged in Discourse (as by now the Discourse Connect plugin has done it’s job to create a new user in Discourse)

For the second form,

  1. The user adds a name and email.
  2. We provide them with a temp password
  3. Rest of the process is the same i.e. the user logs in automatically on sign up
  4. But in this case, the user is not created in Discourse.

I don’t see any setting in Discourse Connect that is triggered by changing a form for registration. If there a hook that needs to be trigged on user registration in WP that is not trigged in the case of second form?

In fact, which hook in WordPress is used to create a user in Discourse? There must be an API call made to trigger activity in Discourse. Could it not fire for some reason?

1 Like

What happens for these users when they click “Log in” in Discourse? Describe exactly what happens when they attempt that. I understand the user is not showing up in your admin panel after they’re created in Wordpress, but this is a slightly different question.

1 Like

First case where user is registered in WP -
The user doesn’t have to click login in Discourse and they are auto-logged in
by clicking on the following link in WP -

If I just use the link - https://community.showprowess.com/ to go to Discourse from WP, the user is not logged in and I have to click the Login button on Discourse to login the user.

Once the user is logged in, they stay logged in until I log out from WP.

This creates a problem because if the user does not click on the /session/sso?return_path=/ , the user is not logged in. This restricts me from directing user to private message page from WP to Discourse. (a functionality I need as part of the product)

For instance, I want to send a private message to user X,
I add this link to their custom post on WP -

As this is the first link I click, the message box does not open. Instead I am logged in to Discourse. Now I have to go back and then click on same link (message link) again to make it work.

It looks like this -

This is annoying for the users.

Earlier, this was all working just fine as the user was automatically logged in to Discourse and the URL https://community.showprowess.com would take me to a logged in Discourse page. May be the user was logged in using Browser cookies or something but that does not work anymore.

When user is not registered on Discourse
This is happening on both forms i.e. the new and old ones.
In this case, I logged back in and went through the onboarding process and this time the user was created in Discourse. Before that, user did not exist in Discourse (I checked the new user list in Admin area before logging in again).
I followed the same steps as above - click on /sessions URL to auto login in Discourse. If I click on just community domain does not login.

Unfortunately, I am not able to reproduce the case that the user is not created on registration but is created on first login. This does not happen on every single new user registration which is very strange.

I hope this helps.

1 Like

To be honest I’m a little confused as to how this description of your issue relates to your previous description of an issue being caused by having two different Wordpress registration forms. But I think I can help you here nonetheless.

One thing to understand is that there is no way (and has never been a way) to be instantly logged in to two different services on two different domains. Whenever it seems like you’re logged into service A on domain A and when you go to service B on domain B you’re also logged in what has actually happened is you’ve been logged in to service B via service A only once you visit domain B and a login process is initiated, not before.

Another thing to understand is that, outside of the specific scenario you’re describing where you want to redirect a person to a specific place in the app which requires a session, most users do not care about, or notice the fact that they need to click “login” on service B sometimes. In my experience working with clients on identity solutions site administrators are typically much more sensitive to this than their users are.

The way this works hasn’t changed. Whenever it seems like a user is “automatically” logged in what actually is happening is they’ve been redirected back to Wordpress and then redirected back to Discourse once their session in Wordpress has been authenticated. If they’re already logged in in Wordpress it will seem like they’ve been “automatically” logged in to Discourse as this redirection will happen without the user needing to do anything.

One way you can trigger the “automatic” login, and redirect them to a specific place in Discourse after login, is by using the path you’ve already shared

https://community.showprowess.com/session/sso?return_path=[any path in Discourse]

If the user is already logged in to Wordpress, but not yet logged in to Discourse when using this URL, this is what will happen:

  1. Discourse automatically starts the DiscourseConnect login process
  2. User’s browser is redirected to wordpress
  3. User is already logged in, so the user is automatically redirected back to Discourse
  4. If there was a return_path value in the URL used in 1 the user will be redirected there

From the user’s perspective they will see their browser loading briefly, but they will effectively be “automatically” logged in to Discourse and redirected to a specific part of the app.

Note that you can actually make the return_path any URL, even a separate domain, if you set the site setting discourse connect allows all return paths to true.

2 Likes

Thank you! This is helpful in solving the issue of auto logging the user from WP to Discourse. I can use the return_path to direct user to any page in Discourse. This solves the issue of directing user to the message page.

However, I am still not sure why, in some cases, the user is not created in Discourse when the user is created in WordPress. Do you know when is the user really created in Discourse from SSO?

  1. Is it when a user is created in WP?
  2. Is it when a new WordPress user tries to access Discourse and then the user is created, logged in and directed to Discourse?

Which hook do we use in WP to create a user in Discourse?

I am trying to understand what edge case would not create a user in Discourse when a user is created in WP.

Practical use case:
I welcome new users every week with a message. Last week 30 users joined on WP and 16 were created in Discourse. When I want to tag them in a welcome message, I can’t tag all of them - which is very strange to me.

Thanks for helping out Angus. I appreciate it.

1 Like

With the default settings users are created in Discourse the first time the user logs into Discourse using DiscourseConnect. Until that moment no user exists in Discourse.

The WP Discourse plugin also has a setting “Create or Sync Discourse Users on Login” which, when enabled, will create a user via the Discourse API after the user registers in Wordpress. This setting uses the wp action wp_login, so your user registration process has to trigger that action for this feature to work.

2 Likes

Now it all makes sense. Thanks, Angus.

I have the “Create or Sync Discourse Users on Login” setting checked. The user is not getting created in Discourse because some users register on WP but do not visit the community on first login. They might come back and login and then click on community URL and then the user is created.

The current auto login after registration on WP does not use WP_login

add_action( 'cred_save_data', 'cred_autologin_V3', 10, 3 );
    
function cred_autologin( $post_id, $form_data ){
  if ( ID1 == $form_data['id']  ) { // Edit as required
    wp_set_current_user( $post_id );
    wp_set_auth_cookie( $post_id );
//    wp_redirect( home_url( '/some-ending-page/' ) );
  //  exit(); 
  }
}

I would prefer to create the user in Discourse on WP registration.
I have custom hooks on the user registration form that can run the API. Is there a code I can add to the custom hook to create a user in Discourse via the API?

This is the part I don’t know how to do i.e. trigger wp_login from a custom hook.

2 Likes

Just add an action trigger to your code.

do_action( 'wp_login' );
2 Likes

So I did exactly what you told me to do.

add_action( 'cred_save_data', 'cred_autologin_V3', 10, 3 );
    
function cred_autologin( $post_id, $form_data ){
  if ( ID1 == $form_data['id']  ) { // Edit as required
    wp_set_current_user( $post_id );
    wp_set_auth_cookie( $post_id );
   do_action( 'wp_login' );

//    wp_redirect( home_url( '/some-ending-page/' ) );
  //  exit(); 
  }
}

But I get the following error:
Message: Uncaught ArgumentCountError: Too few arguments to function WPDiscourse\WordPressEmailVerification\WordPressEmailVerification::verify_email_after_login(), 1 passed in /home/customer/www/domain.com/public_html/wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in /home/customer/www/domain.com/public_html/wp-content/plugins/wp-discourse/lib/wordpress-email-verification.php:128

I have another code that bypasses email verification

add_filter( 'discourse_email_verification', 'disable_discourse_email_verification_prowess');
function disable_discourse_email_verification_prowess() {
  wp_mail( 'himanshu@eshowprowess.com', 'User verified', 'Status must change' ); 
   
    return false;
}

Is this a code sequencing issue or do I need to pass some parameter to the wp_login action?

Edit: The user was created in WP and was logged in but the user was not created in Discourse.

1 Like

I suggest you do some reading on the action. The WP docs are a better resource than me on this. I won’t be able to figure out how best to integrate your custom WP login code here unfortunately. In terms of the issue you’re having, I think we’ve got to the bottom of it.

3 Likes

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