Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display online info


  • poco06
    Participant

    @poco06

    Hello,
    I’d like to display the “online” info inside the user meta in the member loop.
    How can I do this?
    Thanks
    Pat

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

  • danbp
    Moderator

    @danbp

    Can you develop your idea ? Member’s loop is wide….


    poco06
    Participant

    @poco06

    Hello,

    Sorry for the delay in my answer, but in the meantime, I have found a solution if anyone is interested. The aim is to have a specific parameter showing the “state (online or offline)” of a user. Here is a function that can do the trick :

    function is_user_online($user_id, $time=5){
    	global $wp, $wpdb;
    	
    	$user_login = $wpdb->get_var( $wpdb->prepare( "
    		SELECT u.user_login FROM $wpdb->users u JOIN $wpdb->usermeta um ON um.user_id = u.ID 
    		WHERE 	u.ID = $user_id 
    		AND um.meta_key = 'last_activity'
    		AND DATE_ADD( um.meta_value, INTERVAL $time MINUTE ) >= UTC_TIMESTAMP()
    		" ,APP_POST_TYPE
    	));
    	
    	if(isset($user_login) && $user_login !=""){
    		return true;
    	}
    	else {return false;}
    }

    shanebp
    Moderator

    @shanebp

    Please use the code button when posting code.

    Here is a better approach, imo:

    function is_user_online( $user_id, $time=5 ) {
    
       $last_activity = bp_get_user_last_activity( $user_id );
    
       $curr_time = time(); 
    
       $diff = $curr_time - strtotime( $last_activity );
    
       $time *= 60;  // must be in seconds
    
       if( $diff < $time )
          return true;
       else 
          return false;
    }

    poco06
    Participant

    @poco06

    Hello shanebp,

    Thanks for your proposal. I tested it and it is fully working.
    Just one point : do I still have the ability to define which activity time to take into consideration by call the function. Until now, here’s the code I placed for that :

    if(is_user_online($userid, 30))
    with time in minuts (ie : 30 for this example)

    Regards
    Pat


    johnkim1968
    Participant

    @johnkim1968

    Hello shanebp,

    I want to implement this code in the Profile page to show whether the member is online or not. How do I do that? After adding the function to the functions file, how do I call it?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display online info’ is closed to new replies.
Skip to toolbar