Skip to content

Instantly share code, notes, and snippets.

@norcross
Forked from jkudish/always-login.php
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/03971f5535c5092a9976 to your computer and use it in GitHub Desktop.
Save norcross/03971f5535c5092a9976 to your computer and use it in GitHub Desktop.
<?php
/**
* keeps a user always logged in
* don't use this on a production site, ever!
*/
add_action( 'init', 'rkv_auto_login' );
add_action( 'admin_init', 'rkv_auto_login' );
function rkv_auto_login() {
// bail on ajax
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
// bail if the user is logged in
if ( is_user_logged_in() ) {
return;
}
// get the user
$user = get_userdata( 1 ); // 1 being the ID that I want
// bail if I don't have userdata
if ( empty( $user ) || ! is_object( $user ) ) {
return;
}
// set the current user
wp_set_current_user( $user->ID, $user->user_login );
// set the auth cookie
wp_set_auth_cookie( $user->ID );
// now actually log in
do_action( 'wp_login', $user->user_login );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment