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.
Debug info is here: hidden link
Primary error is "Authentication failed"
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 );
}
}
}
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'
);
I actually commented out the lines that create the CPT in case anyone decides to copy-pasta my code 🙂