Skip to:
Content
Pages
Categories
Search
Top
Bottom

checking if certain user is logged in ?

Viewing 10 replies - 1 through 10 (of 10 total)

  • mmrs151
    Participant

    @mmrs151

    Looking for same solution.

    Did you have any luck later?


    danbp
    Moderator

    @danbp

    Please read here for more details:

    Playing with the user’s ID in different contexts


    mmrs151
    Participant

    @mmrs151

    Thanks @danbp

    but I must be missing something from that doc. Not sure how can I know if the displayed user is online right now.

    I however think, it should be a core feature of BP.

    Thanks.


    danbp
    Moderator

    @danbp

    There is a function for that.

    if ( is_user_logged_in() ) {
    
    // do something
    
    } else {
    
    // do something other
    }

    Aside this, you need also to use bp_loggedin_user_id() to get the ID, as explained on the documentation.

    Here a raw example how to implement an online/offline check, at least for inspiration. Be aware that there is probably a better solution! 😉

    /* is user online ? - add to child theme's template entry.php */
    if ( bp_is_active( 'activity' ) ):
    
    $user_id = bp_loggedin_user_id();
    $last_activity = '';
    
    	if( $user_id == bp_get_activity_user_id() ) {
    		$last_activity = bp_get_user_last_activity( $user_id );
    	}
    
    	$curr_time = time(); 
    
    	$diff = $curr_time - strtotime( $last_activity );
    
    	$time = 5 * 60;  // 5mn. Must be in seconds
    
    	if( $diff < $time )
    		echo '<br/><span class="on-line">Online</span>';
    	else 
    		echo '<br/><span class="off-line">Offline</span>'; 
    
    endif;

    mmrs151
    Participant

    @mmrs151

    Thank you so much Dan for your valuable time.

    I already have the online/offline status implementation based on last activity within a time frame ( 5 mins in your example).

    But its not true. The user can log out in last 5 mins and status will still be online as the activity time is within the threshold. Hence I was looking for real solution.

    As for the first part of your answer, it will not work, as the is_user_logged_in() works on current visitor of the site according to the doc, (https://developer.wordpress.org/reference/functions/is_user_logged_in/), which returns a WP_User, current WP_User instance.

    My issue is, I want to display a online/offline status if they are logged in or logged out accordingly on the member list,

    Which feels like a rabbit hole for me now 🙁


    Henry Wright
    Moderator

    @henrywright

    I built a plugin for this recently. It’s called Here:

    Here

    It’s designed for WordPress but feel free to modify so that it works with BuddyPress. The code is also on GitHub


    mmrs151
    Participant

    @mmrs151

    Amazing!

    I will have a look and update the thread for others.


    mmrs151
    Participant

    @mmrs151

    Dear Henry,

    I invested some time on your plugin, it does it very nicely and very clean code.

    However, the solution is kind of what I have now. So for your plugin even if the author/commentator logged out within the expires_in threshold, it will still show the user is online as the transient not expired yet.

    That would be great to have a core functionality like isUserLoggedIn(int $userId): bool

    Is there a way I can request for it to the wordpress/buddypress core developers?

    Thanks


    Henry Wright
    Moderator

    @henrywright

    For WordPress, I had to use a set amount of time for expires_in (instead of hooking to logout) because a user doesn’t have to be logged in when commenting on a post.

    I think the time I set isn’t long so the green dot will disappear after a short period of inactivity.


    Henry Wright
    Moderator

    @henrywright

    I don’t think the core developers would add something like this to core, but feel free to ask on Trac.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
Skip to toolbar