Skip to content

Instantly share code, notes, and snippets.

@qutek
Last active March 28, 2020 21:16
Show Gist options
  • Save qutek/d30b1ac1103334f57c24c60b5cabc442 to your computer and use it in GitHub Desktop.
Save qutek/d30b1ac1103334f57c24c60b5cabc442 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');` )
<?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