I want the user be logged in automatically after filling registration form. I use this code below I found in one thread. The form id is 12788. But the user is not logged in. I tried ajax, no help. The username and nickname are auto-generated. Does that have some bearing on this or is this code somehow wrong?
/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 12788 == $form_data['id'] ) { // Edit as required
if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => $_POST['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() );
}
}
}
}
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
Thank you for getting in touch.
Actually yes it does have some bearing on this because the code is searching for the user_login field.
If this is autogenerated then it won't be on the form.
Could you send me a screenshot of the First and Last name of a user field as well as what the autogenerated login is.
I should be able to modify the code to work for this case.
Thanks,
Shane
Hi Shane, thank you. Sorry I don't understand what you want - the first and last name are the default first and last name user fields. I attach here the user form screenshot, though this probably isn't what you wanted 🙂
But let me know and I will send the right one.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
Actually no this isn't it. I want a screenshot of the generated user profile. The first and the last name.
I want to confirm that the auto generated login is from first and last name of the user.
Thanks,
Shane
🙂 still in dark here, you want this?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
Yes this is what I mean but I need to see the username that is generated as well.
Here is an example. See Screenshot.
Thanks,
Shane
so the username is generated from email start.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
Try this.
/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 12788 == $form_data['id'] ) { // Edit as required
if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_POST['user_email'], 0, strpos( $_POST['user_admin'], "@")),
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
Try this and let me know if it helps.
Thanks,
Shane
Thank you Shane. Unfortunately it doesn't help.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
I forgot to make one change.
Here is the refined code.
/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 12788 == $form_data['id'] ) { // Edit as required
if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_POST['user_email'], 0, strpos( $_POST['user_admin'], "@")),
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
Would you mind allowing me to have admin access to the website to check on this for you ?
The private fields will be enabled for your next response.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Katja,
I tried checking on this with credentials that you've sent but i'm getting error 404 on the link that you've sent. See Screenshot
Could you check on this for me .
Thanks,
Shane
Hi Shane, please delete the screenshot or hide it. The login address has changed too now that the site is published and the domain changed. The new login address is in edited private message.