Support

Account

Home Forums General Issues Show online offline status automatically Reply To: Show online offline status automatically

  • Hi, thank you for your answer. I will give your proposals a try. But this isn’t the actual format though, is it $post_id = "user_{$user_id}";? Looks weird, almost like the javascript template literals. Please correct me if I’m wrong.

    I have found a different possible solution involving the use of get_transient() function that may or may not be a better way of handling this. I’m not sure yet, because I haven’t got it working as of yet.

    I know it’s off topic in this forum but if it could help someaone else with a similar issue, I’ll post my current code here. And just to clarify, my “advisors” are actually a custom post type where each post will have an author (user) connected to them.

    In functions.php:

    <?php
    function is_user_online( $user_id ) {
        // get the online users list
        $logged_in_users = get_transient( 'users_online' );
    
        // online, if user is in the list and last activity was less than 15   minutes ago
        return isset( $logged_in_users[$user_id] ) && ( $logged_in_users[$user_id] >     ( current_time( 'timestamp' ) - ( 15 * 60 ) ) );
    }
    ?>

    And in the template file (in my case this is in a child theme, template partial file):

    <?php
                  if (is_user_online( $post->post_author )) {
                    echo $green_status;
                  } else {
                      echo $red_border_status;
                  } ?>"><?php
                  if (is_user_online( $post->post_author )) {
                    echo 'Available';
                  } else {
                      echo 'Offline';
                  }
    
    // and the card status message
    
    if (is_user_online( $post->post_author )) {
              echo $green_status;
            }
            if (! is_user_online( $post->post_author )) {
              echo $red_fill_status;
            } ?>">
            <?php
            if (is_user_online( $post->post_author )) {
              echo 'online';
            }
            if (! is_user_online( $post->post_author )) {
              echo 'offline';
                  ?>

    This is working in the way that the css selector is actually echoed out, giving the button and the status message color AND text content. However it still does not seem to recognize if the user is actually logged in or not, because it always returns the same value – the css selector for the red color and the “Offline” status text.