Skip Navigation

[Resolved] I am unable to auto login user after registration

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 1 month, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#2403809

Code

add_action( 'cred_save_data', 'cred_autologin_V2', 10, 3 );
    
function cred_autologin_V2( $post_id, $form_data ){
  if ( 1 == $form_data['id'] || 2 == $form_data['id'] || 3 == $form_data['id'] ) { // Edit as required
    if ( !empty( $_REQUEST['user_email'] ) && !empty( $_REQUEST['user_pass'] ) ) {
        
      // get the user credentials from the $_POST object
      $user = array(
        'user_login' => substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")),
        'user_password' => $_REQUEST['user_pass'],
        'remember' => true
      );
      $login = wp_signon( $user, false );
      if ( is_wp_error($login) ) {
        error_log('error during auto login, user_login was: ' . substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")));
        error_log( $login->get_error_message() );
      }
    }
  }
}

This was working until last night and now the user registers but does not get logged in, what could I be missing?

Form code:

[creduserform]
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_messages' class='alert alert-warning']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-6">
			<label>[cred_i18n name='first_name-label']First name[/cred_i18n]</label>
			[cred_field field='first_name' class='form-control' output='bootstrap']
		</div>
		<div class="form-group col-md-6">
			<label>[cred_i18n name='last_name-label']Last name[/cred_i18n]</label>
			[cred_field field='last_name' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-12">
			<label>[cred_i18n name='user_email-label']Email[/cred_i18n]</label>
			[cred_field field='user_email' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-12">
			<label>[cred_i18n name='user_pass-label']Password[/cred_i18n]</label>
			[cred_field field='user_pass' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_submit' output='bootstrap' value='Get Started For Free' class='btn btn-primary btn-lg toolset-home-v2']
		</div>
	</div>
  <br>
</div>
[/creduserform]

I have also look at the server error log and found nothing related to this
2022-06-23 17:18:41 UTC [apache][autoindex:error] [pid 64628] [client 66.249.70.17:12136] AH01276: Cannot serve directory /home/customer/www/domain/public_html/wp-content/plugins/wpdatatables/assets/js/wpdatatables/: No matching DirectoryIndex (index.html,Index.html,index.shtml,index.php,Index.php,index.phtml,Index.phtml,index.htm,Index.htm,home.htm,Home.htm,Default.htm,Default.html,default.htm,default.html) found, and server-generated directory index forbidden by Options directive

2022-06-23 16:41:39 UTC [nginx][error] 55446#0: *7796941 openat() "/home/u2-egtifjgmwrwx/www/domain/public_html/.well-known/assetlinks.json" failed (2: No such file or directory), client: 66.249.70.8, server: domain, request: "GET /.well-known/assetlinks.json HTTP/1.1", host: "www.domain"‮

#2404063

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

During testing your code on my website it failed to login, only if the email address included some special characters like "+".

As special characters can't be used in the username, the logic to extract the username fails:


'user_login' => substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")),

It is a good idea to update this line, to use the email address instead:


'user_login' => $_REQUEST['user_email'],

Another important, point is that if the form is set to submit using AJAX, the code will work in the background to log in the user, but you'll only see the logged-in status when the same page will refresh or if you'll move to another page.

regards,
Waqar

#2404471

I was able to solve it with this code

add_action( 'cred_save_data', 'cred_autologin_V3', 10, 3 );
    
function cred_autologin_V3( $post_id, $form_data ){
  if ( 1 == $form_data['id'] || 2 == $form_data['id'] || 3 == $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(); 
  }
}

Do you see a problem or incompatibility of this code with Toolset?

#2405705

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update.

Your code looks good and works on my test website, as well.

Thank you for sharing it and it will help other users too.

#2408719

My issue is resolved now. Thank you!