Skip to content

Instantly share code, notes, and snippets.

@ambercouch
Last active February 22, 2023 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambercouch/290aada606ad06adeb88de71f19d6f2c to your computer and use it in GitHub Desktop.
Save ambercouch/290aada606ad06adeb88de71f19d6f2c to your computer and use it in GitHub Desktop.
<?php
/**
*******************************************************************************
* MAL: Maintenance Auto-Login.
*******************************************************************************
* Automatically logs you in as the first admin user found in the WordPress
* database.
*
* How to use it:
*
* 1. Add the following 2 lines to wp-config.php - adjust the values
* define( 'MAL_SECRET_USER', 'admin:auto' );
* define( 'MAL_SECRET_PASS', '****' );
* 2. Save this code to wp-content/mu-plugins/auto-login.php
* 3. Now you can login to WordPress by using the SECRET_USER / SECRET_PASS
* combination. When using these credentials you will end up as admin user.
* 4. To disable this plugin again comment out the 2 lines in wp-config.php
*******************************************************************************
*/
if ( ! defined( 'ABSPATH' ) ) { die(); }
if ( defined( 'MAL_SECRET_USER' )
&& defined( 'MAL_SECRET_PASS' )
&& MAL_SECRET_USER
&& MAL_SECRET_PASS
) {
add_filter( 'authenticate', 'mal_auto_login', 3, 10 );
}
function mal_auto_login( $user, $username, $password ) {
if ( MAL_SECRET_USER == $username && MAL_SECRET_PASS == $password ) {
// Find an admin user ID.
$user_id = mal_get_admin_user_id();
if ( ! $user_id ) {
wp_die( 'No admin user found' );
}
// Log in as admin user automatically.
$user = get_user_by( 'id', $user_id );
wp_set_current_user( $user_id, $user->data->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->data->user_login );
wp_safe_redirect( admin_url() );
exit;
}
}
function mal_get_admin_user_id() {
global $wpdb;
$sql = "
SELECT u.ID
FROM {$wpdb->users} u
INNER JOIN {$wpdb->usermeta} m ON m.user_id = u.ID
WHERE
(m.meta_key = '{$wpdb->prefix}user_level' AND m.meta_value = 10)
OR
(m.meta_key = '{$wpdb->prefix}capabilities' AND m.meta_value LIKE '%\"administrator\"%')
";
$res = intval( $wpdb->get_var( $sql ) );
return $res;
}
<?php
/**
*******************************************************************************
* Log in with any password. You only need to know the username or email address.
*
* How to use it:
*
* 1. Save this code to wp-content/mu-plugins/auto-login.php
* 2. Now go to wp-login.php and enter a valid username together with any
* password. The password is not validated, only the username must exist.
* 3. To disable this plugin again simply delete the file from mu-plugins.
*******************************************************************************
*/
add_filter( 'authenticate', 'nop_auto_login', 3, 10 );
function nop_auto_login( $user, $username, $password ) {
if ( ! $user ) {
$user = get_user_by( 'email', $username );
}
if ( ! $user ) {
$user = get_user_by( 'login', $username );
}
if ( $user ) {
wp_set_current_user( $user->ID, $user->data->user_login );
wp_set_auth_cookie( $user->ID );
do_action( 'wp_login', $user->data->user_login );
wp_safe_redirect( admin_url() );
exit;
}
}
-- reset the admin password to password
-- if you need a hash https://www.miraclesalad.com/webtools/md5.php
UPDATE wp_users
SET user_password="5f4dcc3b5aa765d61d8327deb882cf99"
WHERE ID="1";
-- Or if you want to update another user use there username (user_login)
-- WHERE user_login="admin";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment