List currently logged in users (wordpress)

Is there a way to display a list of currently logged in users?

Ideally it would just show their name and gravatar.

Related posts

Leave a Reply

5 comments

  1. Personaly I would use the session_tokens meta key to determine if a user is online (or has a running session) or not.

    $aUsers = get_users([
        'meta_key' => 'session_tokens',
        'meta_compare' => 'EXISTS'
    ]);
    

    You’ll get a list of Users with running sessions. Then you can use that for your output.

    echo sprintf(
        'Users online: %s',
        implode(', ', array_map(function($oUser){
            return get_avatar( $oUser->ID, 96 ) . '<span class="username">'$oUser->display_name.'</span>';
        }, $aUsers))
    );
    

    You could even display the session ip addresses if you modify the last bit of code.

    echo sprintf(
        'Users online: %s',
        implode(', ', array_map(function($oUser){
            $aCurrentSessions = get_user_meta($oUser->ID, 'session_tokens', true);
            return get_avatar( $oUser->ID, 96 ) . '<span class="username">'$oUser->display_name.'</span> (' .
                implode('; ', array_map(function($aSession) {
                    return $aSession['ip']; // only return the session ips
                }, $aCurrentSessions)) . ')';
        }, $aUsers))
    );
    
  2. WordPress does not have a built in function for this, but you can easily add one yourself. For example, you can use the wp_login hook to store each users last login time. Then you can list the users that logged in within the last 30 minutes or so as online.

    add_action('wp_login', 'store_last_login', 10, 2);
    
    function store_last_login($current_user) {
        global $current_user;
        get_currentuserinfo();
        $user = $current_user->user_login;
        update_user_meta($current_user->ID, 'last_login', current_time('mysql', 1));
    }
    

    Then, you can echo out the users that logged in within the last twenty minutes ( or whatever timeframe you choose) with something like:

    function list_online_users() {
    $users = get_users( 'blog_id=1' );
    foreach ($users as $online) {
    
        $getLastLogin = (get_user_meta($online->ID, 'last_login', true));
        $lastLogin = new DateTime($getLastLogin);
        $since_start = $lastLogin->diff(new DateTime(current_time('mysql', 1)));
    
        $minutesSinceLogin = $since_start->i;
    
        // this will list every user and whether they logged in within the last 30 minutes
        if ($minutesSinceLogin > 30 ) {
            echo '<li>'.$online->user_login . ' is offline </li>';
        } else { 
            echo '<li>'.$online->user_login . ' is online </li>'; 
        }
    
        }
    
    }
    
  3. If you don’t want to use a plugin, this can be done easily :

    1/ Add this to your functions.php file to add or remove a usermeta when the user connects or disconnects :

        function add_user_logged_meta(){
            $user = wp_get_current_user();
            update_user_meta($user->ID, 'asdb-loggedin', true);
        }
        // user connects
        add_action('init','add_user_logged_meta');
    
        function delete_user_logged_meta() {
            $user = wp_get_current_user();
            update_user_meta($user->ID, 'asdb-loggedin', false);
        }
        // user disconnects
        add_action('clear_auth_cookie', 'delete_user_logged_meta');
    

    2/ In your templates (you could also make a function in functions.php), to show all users that are loggedin, just use the built in wordpress get_users() with some meta query params :

    $loggedin_users = get_users([
        'meta_key' => 'asdb-loggedin',
        'meta_value' => '1',
        'meta_compare' => '='
    ]);
    foreach( $loggedin_users as $loggedin_user ){
        echo $loggedin_user->user_login;
    
    }
    
  4. 1)Create on extra field named as ‘status’ with enum(‘1′,’0’) in wp_users table
    2)after in functions.php put following code

    <?php
    function add_session_mitesh()
    {
        global $wpdb;
        if(is_user_logged_in())
        {
            $user_id = get_current_user_id();
            $wpdb->query("UPDATE wp_users SET `status` = '1' WHERE `wp_users`.`ID` = $user_id;");
        }
         $user_id;
    }
    add_action('init','add_session_mitesh');
    
    
    function delete_session_mitesh() {
    global $wpdb;
        if(is_user_logged_in())
        {
            $user_id = get_current_user_id();
            $wpdb->query("UPDATE wp_users SET `status` = '0' WHERE `wp_users`.`ID` = $user_id;");
        }
     $user_id;
    }
    add_action('wp_logout', 'delete_session_mitesh');
    ?>
    

    now to list the currently login user you can use

    <?php  $users=$wpdb->get_results( 'SELECT * FROM wp_users where status='1'', 'ARRAY_A'); 
    <table width="100%" border="2px" style="text-align: center;">
        <?php
        foreach( $users as $g )
        {   
        ?>
            <tr><td style="text-align: center;"> <center> <?php echo $g['user_login'] ?> </center></td>
            </tr>
        <?php } ?>
        </p>
        </table>    
    
  5. You can get current logged in user in following way

    $current_user = wp_get_current_user();
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />'; ?>
    

    id this $current_user variable you can find all the user details in array and object you can take from this variable