WordPress code snippet to login user programmatically in your functions.php
file:
clean_user_cache( $user_id );
wp_clear_auth_cookie();
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, true, false );
$user = get_user_by( 'id', $user_id );
update_user_caches( $user );
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.