Last active
October 29, 2024 15:38
-
-
Save AkramiPro/3b9181d04285ec43c978e359ab183d9c to your computer and use it in GitHub Desktop.
Wordpress Login Programmatically Using User ID - (Without a password)
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 | |
// Put this code in mu-plugins or functions.php | |
// then open your site url with add `?switch_user=__strong_random_string__` query to end of the url. | |
// ex: https://test.com/?switch_user=__strong_random_string__ | |
if ( isset( $_GET['switch_user'] ) && $_GET['switch_user'] === '__strong_random_string__' ) { | |
add_action( 'init', function () { | |
$user_id = 1; // in most of case user_id=1 is administrator. | |
wp_clear_auth_cookie(); | |
wp_set_current_user( $user_id ); | |
wp_set_auth_cookie( $user_id ); | |
wp_safe_redirect( admin_url() ); | |
exit(); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment