Skip Navigation

[Resolved] Cred User Form Auto login

This support ticket is created 2 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Puntorosso 2 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2117143

Hi, I use this function to auto login a user when he register through a Cred User form.

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
   
    if ( 640 == $form_data['id'] ) { 
        // Log in with new password
            $user_data = array(
                'user_login'    => $_GET['username'],
                'user_password' => $_GET['pass'],
                'remember'      => true
            );
          $login = wp_signon( $user_data, false );
   
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
   
        }
    }

It works, but if the user already exist, it doesn't login.
Any help is really appreciated.

#2118197

It works, but if the user already exist, it doesn't login.
Right, registration Forms are not meant to be used for standard logins. The automatic login code here is triggered by a successful submission during the cred_save_data event hook. If a User account already exists, the submission is not successful and the cred_save_data event is never triggered. So the code to automatically login the User is never executed if the User account already exists.

I think the only API hook that would be applicable in this case would be cred_form_validate, since cred_save_data, cred_submit_complete, and cred_before_save_data all require a successful submission.

You can display a login form instead with the wpv-login-form shortcode:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-login-form
It is preferred to send existing Users to a login form instead of a Create User Form or an Edit User Form, since these 3 types of forms all serve different purposes.

#2118217

Ok understood.
The problem is that the login should happen automatically, using only an url like

hidden link"JOHNDOH123"

That's why I was trying to use Cred, as it would allow me to populate the fields from URL parameters.

#2118271

Hmm, okay I see what you mean. Still, Toolset User Forms are not practical for logins. I cannot think of an elegant way to do this with built-in features of the wpv-login-form shortcode. The form generated by the wpv-login-form shortcode is not designed to respond to URL parameters. I think the only way would be to use custom JavaScript to get the URL parameter value and set the login form input value programmatically after the page loads.

To get URL parameter values:
https://www.sitepoint.com/get-url-parameters-with-javascript/

To set input values:
https://stackoverflow.com/questions/5700471/set-value-of-input-using-javascript-function

Wrap it all in a document ready handler like:

jQuery(document).ready(function($){
  // add your custom code here
});
#2118283

Would it be possible to enable the use of the Cred edit user form also for guest users?

hidden link

#2118311

Unfortunately no, there is no easy way to override this setting. Guest Users have very limited capabilities in User Forms, they can only create new User accounts. It would be easier to set up an Edit Post Form that includes some generic fields, then use the Forms API to programmatically edit a User profile using those generic field values. You could manage Guest Access to that Edit Post Form instead.

#2118349

I adapted some snippets, and now it's working.
went almost crazy because at first no auto login snippet worked.
Finally, I have found out that WooCommerce was blocking the auto-login functions.
I had to use

wp_set_current_user( $user_id, $loginusername );
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );
do_action( 'wp_login', $loginusername, $user );
wp_safe_redirect( $url );
exit;

to make it work.

I think this was also why the die() function didn't fired (question on my other ticket).

Thanks for your support. Best

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.