Using the following snippet in your theme’s functions.php file, you will be able to log-in a WordPress user programmatically.
1 2 3 4 5 6 7 8 9 10 11 12 | function auto_login( $user ) { $username = $user; if ( !is_user_logged_in() ) { $user = get_userdatabylogin( $username ); $user_id = $user->ID; wp_set_current_user( $user_id, $user_login ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_login ); } } Once done, now to log-in a user, just simply do the following: auto_login( 'admin' ) |
Snippet Source/Credit: emoticode