Skip to content

Instantly share code, notes, and snippets.

@palpalani
Last active December 26, 2015 13:19
Show Gist options
  • Save palpalani/7157162 to your computer and use it in GitHub Desktop.
Save palpalani/7157162 to your computer and use it in GitHub Desktop.
Log in a WordPress user programmatically
<?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