-
-
Save phillipwilhelm/d4664229451e2afbe948f2754ebad463 to your computer and use it in GitHub Desktop.
[ Force Login ] Force login WordPress user without password, use it as plugin or place the code under mu-plugins / wp-config.php ( after `require_once(ABSPATH . 'wp-settings.php');` )
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 | |
/** | |
* Auto login user without password, access with <code>http://domain.com/?force_login=username</code> | |
* [email protected] | |
*/ | |
if ( ! defined( 'WP_CLI' ) ) { | |
function force_login() { | |
if( !isset($_GET[ 'force_login' ]) || empty( $_GET[ 'force_login' ] ) ) | |
return; | |
// get user | |
$user = get_user_by('login', $_GET[ 'force_login' ] ); | |
if ( !is_wp_error( $user ) ) { | |
// logging in user | |
wp_clear_auth_cookie(); | |
wp_set_current_user ( $user->ID ); | |
wp_set_auth_cookie ( $user->ID ); | |
$redirect_to = user_admin_url(); | |
wp_safe_redirect( $redirect_to ); | |
exit(); | |
} | |
} | |
add_action( 'template_redirect', 'force_login' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment