I would like for my users to be able to login just by entering their username without password and without 2 factor authentification (no registration possible)
I know it's not good practice, but access to the website will be only possible from a specific location so there is no security issue.
I have found the following code here
// First get the user details
$user = get_user_by('login', $username );
// If no error received, set the WP Cookie
if ( !is_wp_error( $user ) )
{
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID ); // Set the current user detail
wp_set_auth_cookie ( $user->ID ); // Set auth details in cookie
$message = "Logged in successfully";
} else {
$message = "Failed to log in";
}
echo $message;
But i have no idea how and where to implement.
Could i just give everybody the same dummy password and fill it automatically/hidden? Again security is not an issue here, we just want the user to log in to track activity later.
CodePudding user response:
you can use this code
function auto_login_user() {
if ( wp_doing_ajax() ) {
return;
}
if(isset($_REQUEST['user_id_ak']) && $_REQUEST['user_id_ak'] > 0){
$user_id = $_REQUEST['user_id_ak'];
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( admin_url() );
exit;
}
}
add_action( 'init', 'auto_login_user' );
CodePudding user response:
I can tell you how to login via URL (not exactly what you want, but you may find it useful).
Place the following code at the very beginning of functions.php (you can find it in your theme folder), right after "<?php":
if( isset($_GET['username']) and $_GET['pass'] ) {
$user = get_user_by('login', $_GET['username']);
if ( $user && wp_check_password( $_GET['pass'], $user->data->user_pass, $user->ID) ) {
wp_set_current_user($user->ID, $user->user_login);
wp_set_auth_cookie($user->ID);
do_action('wp_login', $user->user_login);
// redirect to admin-area:
wp_redirect( admin_url() );
// redirect to home-page
// wp_redirect( home_url() );
exit;
}
wp_redirect( home_url() );
exit;
}
Login URL => https://SITENAME.XXX/wp-admin?username=XXXXX&pass=XXXXX
Please note you can choose where users will be directed.