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.
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.
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.
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
});
Would it be possible to enable the use of the Cred edit user form also for guest users?
hidden link
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.
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