I trying to (override the) login by code.
I found several examples to do this.
So I login end then redirect the user to a different page.
With every example i tried it doesn't override the login.
So I see with $current_user->user_login that I'm logged in but when I redirect to another page I'm logged out or I'm logged in as the previous user.
Does anybody know why the the user doesn't remain logged in?
Example:
function auto_login() {
if (!is_user_logged_in()) {
//determine WordPress user account to impersonate
$user_login = 'help';
//get user's ID
$user = get_userdatabylogin($user_login);
$user_id = $user->ID;
//login
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action('wp_login', $user_login);
}
}
add_action('init', 'auto_login');