Today’s post will be brief, and I’ll teach you how to add a useful feature to your WordPress site. This can be quite useful if applied properly. This is intended for usage on WordPress sites that provide users with a generic account to login to. On many backend WordPress demos, for example, the author will establish a ‘demo’ account for potential customers to login to and explore with his or her product. Frequently, the author will just post a notice that says;
USERNAME: demo
PASSWORD: demo
This is a bit of a cliche. Isn’t it possible to make the demo look more sleek and professional? I’ll offer you a short snippet to login to the WordPress admin with these credentials automatically. This can be included in your theme’s functions file or placed in its own plugin file and enabled.
This could be useful if;
- Your site has a generic account for anonymous users to login to.
- You want a ‘one click’ login link.
- You want to maximize product demo conversion by providing a quick and efficient pathway to the demo, minimizing required steps.
- You want to direct visitors directly to the relevant location (eg. Settings page).
IMPORTANT: Never use this to login to accounts with real power; eg. Administrator/Editor accounts etc.
Use this snippet carefully.
So, like with my past courses, I’ll start by giving you the whole, annotated code so that those of you who don’t like reading may jump right in and start experimenting. The snippet below has been made to look like a standalone plugin (my personal preference).
/*
Plugin Name: Auto Login
Plugin URI: https://themedev.net/
Version: 1.0.0
Author: ThemeDev
Author URI: https://themedev.net/
*/
function themedev_autologin() {
// PARAMETER TO CHECK FOR
if ($_GET['autologin'] == 'demo') {
// ACCOUNT USERNAME TO LOGIN TO
$creds['user_login'] = 'demo';
// ACCOUNT PASSWORD TO USE
$creds['user_password'] = 'demo';
$creds['remember'] = true;
$autologin_user = wp_signon( $creds, false );
if ( !is_wp_error($autologin_user) )
header('Location: wp-admin'); // LOCATION TO REDIRECT TO
}
}
// ADD CODE JUST BEFORE HEADERS AND COOKIES ARE SENT
add_action( 'after_setup_theme', 'themedev_autologin' );
USAGE – using this URL for login
This is very simple to use. The account username and password are specified in the plugin file (code above), and to login, you simply need to visit; http://example.com/wp-login.php?autologin=demo
You should be taken to wp-admin and logged into the desired account right away. However, if the credentials are incorrect, you should see the login form as usual.
CUSTOMIZE – Use another author
This snippet is very easy to customize. There are essentially only 3 things you will need to edit, and all these changes are to make in the following code block (lines 11 to 17 of the complete code)
if ($_GET['login'] == 'dummy_account') {
// ACCOUNT USERNAME TO LOGIN TO
$creds['user_login'] = 'dummy';
// ACCOUNT PASSWORD TO USE
$creds['user_password'] = 'pa55word';
}
The conditional verification for the URL argument may be found on the first line. The code block above will be looking for;wp-login.php?login=dummy_account
These values can be whatever you want, but original WordPress parameters like ‘loggedout,’ ‘action,’ and’redirect to’ should be avoided. The username to login with is specified on line 4; the customized code snippet will attempt to login using the ‘dummy’ account. Line 7 is where you specify the password, as you might expect. ‘pa55word’ is the password mentioned above. If you fill up these values, you should be set to go!
EXTENSIONS – Use multiple author
Our snippet is currently only set up to auto-login to one account. What if we need separate login links for each account? This is a quick and simple method that does not require you to repeat the entire excerpt again and over. Take a look at my solution below.
/*
Plugin Name: Auto Login
Plugin URI: https://themeedev.net/
Description: Create convenient auto-login links to quickly login to generic accounts. Configure source code to make changes.
Version: 1.0.0
Author: ThemeDev
Author URI: https://themeedev.net/
*/
// Declare global var's
global $login_parameter, $accounts;
// THE PARAMETER TO CHECK FOR
// eg. http://exmaple.com/wp-login.php?param_name=account
$login_parameter = "autologin";
// Another example iteration
$accounts[] = array(
"user" => "campaign",
"pass" => "campaign",
"location" => "wp-admin/admin.php?page=nxc-dashboard",
);
$accounts[] = array(
"user" => "next3",
"pass" => "next3",
"location" => "wp-admin/admin.php?page=next3aws#ntab=settings",
);
$accounts[] = array(
"user" => "nextwoo",
"pass" => "nextwoo",
"location" => "wp-admin/admin.php?page=nextwoo#ntab=features",
);
// SEE PREVIOUS EXAMPLE FOR DETAILS ABOUT THIS FUNCTION
function th_autologin() {
global $login_parameter, $accounts;
foreach ($accounts as $account) {
if( !isset($_GET[$login_parameter]) ){
continue;
}
if ($_GET[$login_parameter] == $account['user']) {
$creds['user_login'] = $account['user'];
$creds['user_password'] = $account['pass'];
$creds['remember'] = true;
$autologin_user = wp_signon( $creds, '' );
if ( !is_wp_error($autologin_user) )
header('Location: ' . $account['location']);
}
}
}
add_action( 'after_setup_theme', 'th_autologin' );
This is essentially the same, but with a foreach loop and an accounts array tossed in for good measure. The autologin() function has the same structure as the login() method, with the exception that the code is repeated (via the foreach loop) for each account. The global array now has all important information. The example above shows how to set up two accounts, but our snippet can handle as many as we need. Simply customize and add as many of the following code blocks as you need to add extra accounts.
// ACCOUNT CODE BLOCK
$accounts[] = array(
"user" => "anotheraccount",
"pass" => "public_password",
"location" => "https://themedev.net/",
);
// END ACCOUNT CODE BLOCK
You will also notice I have moved the parameter name to a global variable as well: this is not necessary, but I did so simply to remove all hard-coded values from the th_autologin() function.
CONCLUSION
Although this snippet is merely a simple function intended for light use, such as on a product demo site, it has the potential to be utilized for far more complex login scenarios. The remainder of the code should be self-explanatory, but if you have any questions about what I did or why I did it, please leave a comment below or send me a tweet. Leave a remark in the space below if you have any feedback, recommendations, or thoughts!
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
