Skip to content

Instantly share code, notes, and snippets.

@Rajinsharwar
Created October 11, 2023 01:32
Show Gist options
  • Save Rajinsharwar/0e0264be1967bddce1cd90117e410306 to your computer and use it in GitHub Desktop.
Save Rajinsharwar/0e0264be1967bddce1cd90117e410306 to your computer and use it in GitHub Desktop.
A WordPress auto-login php script. Upload this file anywhere in your website, and simply login by visiting the file from your browser. For example, if you upload this file in your ROOT, visit "http://your-domain.com/auto-login.php" to automatically login to your WordPress dashboard.
<?php
require_once('wp-load.php');
$loginusername = 'admin'; //Replace your username for which user you want to login.
$user = get_user_by( 'login', $loginusername );
$user_id = $user->ID;
wp_set_current_user( $user_id, $loginusername );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $loginusername, $user );
wp_redirect( admin_url() );
unlink(__FILE__);
?>
@rajinsharwar2
Copy link

Alternate solution:

<?php
require_once( 'wp-load.php' );

$username = 'admin';

if ( !($user_to_login = get_user_by( 'login', $username )) ) {
	if ( !( $user_to_login = get_user_by( 'email', $username )) ) {
	   $admin_users   = get_users( array( 'role' => 'administrator' ) );
       $user_to_login = is_array( $admin_users ) && isset( $admin_users[0] ) ? $admin_users[0] : false;
    }
}

if ( $user_to_login instanceof WP_User ) {
	wp_set_current_user( $user_to_login->ID, $user_to_login->user_login );
	wp_set_auth_cookie( $user_to_login->ID );
	do_action( 'wp_login', $user_to_login->user_login, $user_to_login );
}

unlink( __FILE__ );
wp_redirect( admin_url() );


?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment