Enjoy upto
80% OFF

Automatic login to WordPress Admin

Automatic login to WordPress Admin

Do you want to know the automatic login to WordPress admin?

Today’s blog 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 log in.

On many backend WordPress demos, for example, the author will establish a ‘demo’ account for potential customers to log in to and explore with his or her product.

Frequently, the author will just post a notice that says;

USERNAME: demo
PASSWORD: demo

In this article, we’ll brief how to create a system for automatically login to the WordPress Admin panel, with just a single link click.

How to complete automatic login to WordPress Dashboard?

I’ll offer you a short snippet on automatic login to WordPress admin with these credentials. This can be included in your theme’s functions file or placed in its 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 the required steps.
  • You want to direct visitors directly to the relevant location (e.g. Settings page).

IMPORTANT: Never use this to login to accounts with real power; eg. Administrator/Editor accounts etc.
Use this snippet carefully.

Now create a simple plugin for automatic login to WordPress Dashboard.

Create a plugin folder on your WordPress directory: wp-content > plugins

  1. create a new folder like: auto-login
  2. open this folder and create a new PHP file like: auto-login.php
  3. copy this code and paste.

Customize code for an author

/*
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'); // redirect to
	}
}
// ADD CODE JUST BEFORE HEADERS AND COOKIES ARE SENT
add_action( 'after_setup_theme', 'themedev_autologin');

These values can be whatever you want, but original WordPress parameters like ‘logged-out,’ ‘action,’ and ‘redirect to’ should be avoided.

Customize code for multiple authors

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.

Using URL for automatic login to WordPress Dashboard

This is very simple to use. The account username and password are specified in the plugin file (code above), and to log in, 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.

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.

imagify
Golam Mostafa
Written by

Golam Mostafa

I'm a senior plugin developer for WordPress. I have four years of experience working as a software engineer for ADB Bank, and in 2018, I started developing plugins and founded ThemeDev.

Table of Content

Table of Contents

LTD Deals: 10+ Pro Plugins

Limited Time Offer
LTD Deals: 10+ Pro Plugins

Offer ends soon! Don't let it get away this time!

(NO RECURRING FEES)

Trusted by 25,099+ Customers

30 days Money Back Guarantee