Yes, you can. However, you’ll need to know either the userid or the username.
Create a link with any of that as a data attribute and make a ajax call with that. On success, you can just reload the page and your user will be logged in.
If you know the user id..
function wpse_304392_login_by_id() {
$user_id = $_POST['user_id'];
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
wp_die(); //Essential for AJAX calls
}
If you know the username..
function wpse_304392_login_by_username() {
$username = $_POST['username'];
$user = get_user_by( 'login', username );
$user_id = $user->ID;
if( $user ) {
wp_set_current_user( $user_id, $username );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $username );
}
wp_die(); //Essential for AJAX calls
}
Don’t forget do a nonce check to make it secure.