<?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?
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.
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.
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:
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.
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.