Last active
August 26, 2021 17:55
-
-
Save AkramiPro/3b9181d04285ec43c978e359ab183d9c to your computer and use it in GitHub Desktop.
Wordpress Login Programmatically Using User ID - (Without a password)
This file contains 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 | |
// theme open your site url with add switch_user query to end of the url. | |
// EX: http://test.com/?switch_user=true | |
if ( isset( $_GET['switch_user'] ) && $_GET['switch_user'] ) { | |
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