0

Source: How to check if a user (not current user) is logged in?

I'm using a transient that's created when users login.

$logged_in_users = get_transient('users_online');

Now i'd like to create an extra function that lists all users that are currently online.

My code so far...

function wp_current_online_users(){

    $logged_in_users = get_transient('users_online');


    $args = array(
        'orderby'   =>  'display_name',
        'order'     =>  'ASC',
    );

    $user_query = new WP_User_Query( $args );

    // User Loop
    if ( ! empty( $user_query->results ) ) {

        foreach ( $user_query->results as $user ) {         
            $avatar = get_avatar( $user->ID, 40 );              
            echo '<span class="wp-user-'.$user->display_name.'">' . $avatar . ' </span>';               
        }

    } else {
        esc_html_e('There are currently no users logged in.', 'text-domain');
    }       

}

How to get implement the transients inside the query so that it will display online users only?

0

Seeing that the referenced answer stores users_online as an array of timestamps indexed by user ID, you can use the include argument for WP_User_Query:

$args = array(
    'include' => array_keys( $logged_in_users ),
    // Other arguments
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.