Skip to content

Instantly share code, notes, and snippets.

@eagl3s1ght
Forked from jacks0n/wp-auto-login.php
Last active August 26, 2019 00:45
Show Gist options
  • Save eagl3s1ght/4a409f27cd2c3f58616fc0b4650b28b6 to your computer and use it in GitHub Desktop.
Save eagl3s1ght/4a409f27cd2c3f58616fc0b4650b28b6 to your computer and use it in GitHub Desktop.
Automatically login to WordPress, with a given user and optionally whitelist IPs. Add this to the bottom of wp-config.php, or your theme functions.php. To automatically login, visit the admin login page (/wp-login.php or /wp-admin/).
<?php
/**
* @package automatic_user_login
* @version 1.0.0
*/
/*
Plugin Name: automatic_user_login
Plugin URI: http://wordpress.org/plugins/automatic_user_login/
Description: Automatically logs in a visitor when accessing the admin login area (/wp-login.php)
Author: Jackson Cooper
Version: 1.0.0
Author URI: https://gist.github.com/jacks0n/743a45a98d74da23c8f2
*/
/**
* Automatically logs in a visitor when accessing the admin login area (/wp-login.php)
*
* @author Jackson Cooper <jackson@jacksonc.com>
* @copyright Copyright (c) 2014, Jackson Cooper
* @license MIT
*
* https://gist.github.com/jacks0n/743a45a98d74da23c8f2
*
* Whitelist IPs: add IPs to whitelist in $ip_whitelist. If it is empty, it will allow all IPs.
* Username: Specify the username to login as with the "user" GET parameter (eg. ?user=admin).
* If the "user" get parameter is not set, $default_user_login will be used. If set
* to "*", it will login as the first administrator found. Otherwise it will use the
* value set.
*
* @note If already logged in, or just logged out, it will do nothing.
*/
function automatic_user_login() {
// Already logged in, not necessary
if (is_user_logged_in()) {
wp_redirect(admin_url());
return;
}
// IP whitelist. If this is empty, whitelisting will be disabled.
$ip_whitelist = array('127.0.0.1', '::1', '');
// Default user to login as.
// If this is "*", the first administrator user will be used.
// If the "user" GET parameter is set, this will be used.
$default_user_login = '*';
$ip_blocked = (!empty($ip_whitelist) and !in_array($_SERVER['REMOTE_ADDR'], $ip_whitelist));
$user_logged_out = (isset($_GET['loggedout']) and $_GET['loggedout'] === 'true');
if (($ip_blocked) // IP not whitelisted
or ($user_logged_out)) { // User just logged out
return;
}
// Fetch the user to login as, if it exists
$user_login = (isset($_GET['user'])) ? $_GET['user'] : $default_user_login;
if ($user_login === '*') {
$user = current(get_users(array('role' => 'administrator')));
if ($user === false) wp_die(__( 'ERROR: No admin users exist.'));
} else {
$user = get_user_by('login', $user_login);
if ($user === false) {
$admin_users = get_users(array('role' => 'administrator'));
$admin_users_atr = implode(', ', array_map(function($admin_user) {
return $admin_user->data->user_login;
}, $admin_users));
wp_die(__("ERROR: User '$user_login' does not exist. Other administrators: $admin_users_atr"));
}
}
// Login as $user and re-load / re-direct to the admin page
$user_id = $user->ID;
wp_set_current_user($user_id, $user->user_login);
wp_set_auth_cookie($user_id, true);
do_action('wp_login', $user->user_login);
wp_redirect(admin_url());
}
add_action('login_init', 'automatic_user_login');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment