skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    I finally managed to do what i was looking for! Thanks a lot for your help.

    Here is the final code:

    1. add if statement to see if the user is already login
    2. create a very simple form
    3. add if statement to see if the form is submitted
    4. set current user and auth cookie

    The code is not perfect but it will do the work! Thanks again!

    <?php 
        global $user_id;
        global $wpbd; 
        if (!$user_ID){
    
            if($_POST){ //when form is submitted
                $username = $wpdb->escape($_POST['username']);
                $user = get_user_by( 'login', $username );
                if ( $user === false ) {
                        echo 'no'; // add error message
                } else {
                        //user id exists
                        wp_set_current_user($user->ID, $user->user_login);
                        wp_set_auth_cookie($user->ID);
                        
                        // redirect to admin-area:
                    wp_redirect( home_url() );
                        exit();
                }
            } else {
            
                // user is not log in ?>
            <form method="post">
                <p>
                    <label for="username">User ID</label>
                    <input type="text" id="userame" name="username" placeholder="User ID">
                </p>
                <p>
                    <button type="submit" value="submit" name="submit">Log IN</button>
                </p>
            </form>
            <?php }
            } else {
                wp_redirect( home_url() );
            }
         ?>
    

  2. 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' );
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search