Skip to content

Instantly share code, notes, and snippets.

@AkramiPro
Last active August 26, 2021 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkramiPro/3b9181d04285ec43c978e359ab183d9c to your computer and use it in GitHub Desktop.
Save AkramiPro/3b9181d04285ec43c978e359ab183d9c to your computer and use it in GitHub Desktop.
Wordpress Login Programmatically Using User ID - (Without a password)
<?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