Skip Navigation

[Resolved] Auto Login not working

This support ticket is created 6 years, 2 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.

Our next available supporter will start replying to tickets in about 1.83 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by mayurJ 6 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1105689

I am trying to:
Use the information provided here: https://toolset.com/forums/topic/automatically-log-in-user-after-created/

Link to a page where the issue can be seen:
hidden link

I expected to see:
After signing up, the user should be taken to the account page (/account). The account page does work. You can try logging in yourself with the email and password to verify yourself.

Instead, I got:
404 error. The 404 error is shown because the account page is managed by Access. So only logged in users can see this page.

#1105690

Debug info is here: hidden link

Primary error is "Authentication failed"

#1106283

Got it working.. BUT...can you review and let me know if there is any bad code here (or a better way to make it work)

[php]
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==635)
{

$artist_name = $_POST['first_name'];
$artist_email = $_POST['user_email'];

$my_post = array(
'post_title' => $artist_name,
'post_type' => 'makeup-artist',
'post_author' => $post_id,
'post_status' => 'draft'
);

if ($_POST["user_email"]) {
// $new_post_id = wp_insert_post($my_post);
// update_post_meta( $new_post_id, 'wpcf-artist-email-address', $artist_email );

$user_login = $artist_email;
$user_email = $artist_email;
$user_password = esc_attr($_POST["user_password"]);

$creds = array();
$creds['user_login'] = $user_login;
$creds['user_password'] = $user_password;
$creds['remember'] = true;

// $userID = $user->user_ID;
// $userID=$post->post_author;

$userID = get_post_field( 'post_author', $new_post_id );

//echo "User ID is " . $userID;

$user = wp_signon( $creds, false );
wp_set_current_user( $userID, $user_login );
wp_set_auth_cookie( $userID, true, false );
do_action( 'wp_login', $user_login );

}

}
}

#1106464

Hello,

Thanks for sharing the codes, it will help other users.

I think your codes are good, and if you don't need to create new "makeup-artist" post on user creating, you can remove those codes:

$my_post = array(
'post_title' => $artist_name,
'post_type' => 'makeup-artist',
'post_author' => $post_id,
'post_status' => 'draft'
);
#1106487

I actually commented out the lines that create the CPT in case anyone decides to copy-pasta my code 🙂