Skip Navigation

[Resolved] Auto login doesn't work

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by Minesh 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1247721

I've added an auto login option like so:

<?php
/**
 * Auto-login new CRED user
 */
 add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );

 function tssupp_cred_autologin( $post_id, $form_data ){

     if ( 31 == $form_data['id'] ) { // Edit as required


         if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {

             $user_login = explode("@",$_POST['user_email']);

             // get the user credentials from the $_POST object
             $user = array(
                 'user_login'    =>   $user_login[0],
                 'user_password' =>   $_POST['user_pass'],
                 'remember'      =>   true
             );
             $login = wp_signon( $user, false );

             if ( is_wp_error($login) ) {
                 error_log( $login->get_error_message() );
             }

         }
     }
 }

Details:
1. The user chooses his own password and the username gets auto generated in the form

2. The form is set-up as a multiple step form. In which the user selects a user role. If they select employer the user gets to see the user registration form for employers. If they select freelancer they get to see the user registration form for freelancers. I've done this with a small jquery script to hide / show the right form based on the radio button that's been selected.

3. I'm using the theme Understrap and made a custom page template with my own header (not the wp_header) could this cause an error?

Is there a better way of doing this or am I doing something wrong?

#1247753

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - what if you try to use the following code:

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
 
 function tssupp_cred_autologin( $post_id, $form_data ){
 
     if ( 31 == $form_data['id'] ) { // Edit as required
  
         if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
 
            // $user_login = explode("@",$_POST['user_email']);
          $user = get_user_by( "email", $_POST['user_email'] );
 
             // get the user credentials from the $_POST object
             $user = array(
                 'user_login'    =>   $user->user_login,
                 'user_password' =>   $_POST['user_pass'],
                 'remember'      =>   true
             );
             $login = wp_signon( $user, false );
 
             if ( is_wp_error($login) ) {
                 error_log( $login->get_error_message() );
             }
 
         }
     }
 }

Does that help?

If this does not work I need access details and problem URL where you added the form.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1248963

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I would like to know did you added the code I shared anywhere? If yes, where you added that code as I checked theme's functions.php file but I do not see the code there.

#1249059

Yes. themes/wco/inc/auto-login.php

Contains the code. It's included within the functions.php

#1249455

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now: hidden link

I've moved the code that you added at: themes/wco/inc/auto-login.php to Toolset's "Custom Code" section.
=> hidden link

Now, I can see the page is redirected successfully.

#1249489

The redirect always worked.

It doesn't log in the user after registration, which the code intends to do

#1249507

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - it looks like issue from your theme or how you setup your form.

I've created this test page where I've added the same Toolset form which you are using to register the "Employer"
=> hidden link

I see once I submit the form its successfully logged in. I'm able to see the black admin bar after the redirection. Please check the following screenshot:
=> hidden link

I also checked that the code to loign is also working just fine:

 $login = wp_signon( $user, false );

I also confirm that the $login variable is having the WP_USER object once you register the user. The issue does not look like from Toolset but somewhere else.
=> https://codex.wordpress.org/Function_Reference/wp_signon

#1249553

I see, that's what I suspected. Could it have to do with the fact that the page /aanmelden actually contains 2 user registration forms. However 1 of them is hidden depending on the user role option that's picked?

Could I tackle this by having the user being redirected to a page when picking an option instead? Since toolset currently doesn't allow a user role to be chosen inside a registration form.

I basically did the following:
1. HTML form with 2 radio buttons
2. User picks employer or freelancer
3. depending on the choice the user registration form for either user role is shown, the other one is hidden inside a div using jquery.

#1250605

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could I tackle this by having the user being redirected to a page when picking an option instead? Since toolset currently doesn't allow a user role to be chosen inside a registration form.
=> I think its a good idea that on selection of option you should redirect user to that specific selected registration form and regarding assignment on Role, we can assign the role using the Toolset Form's hook cred_save_data on fly.

I suggest to build a flow like that put both forms on specific two pages and on selection redirect user to that specific page and we can handle it from there.