Always logged in, using mu-plugins

I’ve decided to start posting handy/helpful code snippets here [Idea/inspiration from Bill Erickson].

So here’s my fist snippet! It allows you to remain always logged in when developing sites (Warning: do not use this on a production site!)

I like to put this code in the special mu-plugins folder that way it always runs. We hook into ‘admin_init’ and ‘init’ function, check if we’re logged in, and if not, logs us in. Change the 1 in the get_userdata() function to match the user ID that you want.

* keeps a user always logged in
* don't use this on a production site, ever!
add_action('init', 'auto_login');
add_action('admin_init', 'auto_login');
function auto_login() {
if (!is_user_logged_in()) {
$user = get_userdata(1); // 1 being the ID that I want
wp_set_current_user($user->ID, $user->user_login);
do_action('wp_login', $user->user_login);

Again, don’t use this on a production or even staging site. It’s only meant to be used in a local environment, where you are the sole developer/user of the site (since it always logs into the same user). Use it with caution, you’ve been warned!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: