I was trying to create a custom front-end login page. I know there are many front end login plugins like profile builder, but I need to develop a custom one as per the special client requirement of a read-only username that gets populated by the username passed as URL parameter and then upon entering password and submit, we need to log the user in and redirect him to WordPress profile page. That's why I created a custom login page as a theme template.
On submit I added this code after verifying the password to log the user in and redirect to profile page
wp_set_current_user( $user_info->ID, $user_info->user_login );
wp_set_auth_cookie( $user_info->ID, true, false );
do_action( 'wp_login', $user_info->user_login );
if (is_user_logged_in()) {
wp_safe_redirect('/wp-admin/profile.php');
}
Sometimes this works perfectly and the profile page get loaded. But sometimes, it's not working and get's redirected from profile page to wp-login.php
I even tried adding the below code
$creds = array();
$creds['user_login'] = $user_info->user_login;
$creds['user_password'] = $user_pass;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
But no use.
I am stuck at this as I don't see anything else other than what I tried in the internet to programmatically log a user in Wordpress. But it's sometimes not working. The user is identified as logged in in front end all the time (when checking with is_user_logged_in() or retrieving currently logged in user in the front end). But the backend is sometimes not identifying it as logged in.
Please help me figure out what the issue is and how to fix it