Last active
December 26, 2015 13:19
-
-
Save palpalani/7157162 to your computer and use it in GitHub Desktop.
Log in a WordPress user programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Here is the function, drop it in your functions.php file | |
*/ | |
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 ); | |
} | |
} | |
/* | |
* Then, to log in a user, do the following | |
* Replace 'admin' with your username. | |
*/ | |
auto_login( 'admin' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment