I am trying to programmatically log in a wordpress user, using the following function for that.
public function auto_login( $user ) {
$username = $user;
if ( !is_user_logged_in() ) {
$user = get_user_by( 'login', $username );
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID );
do_action( 'wp_login', $user->user_login );
}
}
Unfortunately it seems not to be working. That function is called from within a shortcode. Could this be the explanation ? Should I rather hook my function to some filter, before any content is output ? Also I would like to redirect the newly logged in user to a specific post based on a $_GET parameter, can I simply add a header redirect at the end of the function ?
Thank you in advance for your help !