Check if user is online?

WordPress doesn’t track if a user is online. It can only know if the current is online, which is always yes ( else there would be no request ). If you want to know if a different user that isn’t the current user is online, that information doesn’t exist, and you’ll need to implement it yourself.

is_user_logged_in checks if the current user is authenticated aka logged in, it’s not a test if a user is online

In order to do this you’re also going to have to make compromises, and realtime online or not status tracking is not something you can do solely within WordPress.

Your options include:

  • Updating a timestamp every time you access a page in user meta, then using that timestamp as a test ( aka if a users timestamp is within the last 5 minutes consider them online ), have an AJAX call ping your site to refresh the timer. Note that if the user closes their browser just after loading a page, they’ll still show online for 5 minutes, and there’s a performance cost to doing this, it will not scale with traffic
  • Having a flag that indicates a user is online, and setting it to true when a user hits any page. Have a cron job that sets all flags to false at regular intervals. This will be cheaper to query on, but it’s more expensive to run. You can make it less expensive by increasing the time delay of cron jobs, thus reducing accuracy. This still makes logged in users more expensive as every page load includes a database write. It will not scale with traffic.
  • Use an external service and set up a Heroku/Simperium/Node server that’s independent of WordPress and tracks online state. How you would do this is beyond the scope of this site as you’re moving into other areas of expertise outside of WordPress. This is the most complex and expensive option, but it does mean your WordPress install is not adversely impacted. It does mean that the online presence data is unavailable in WordPress itself, you will not be able to query unless you build mechanisms to do so which will be expensive. The code that makes the icon green will need to be in javascript on the browser. The worst case scenario here however is that your presence logic fails as your external server buckles under load, but your WordPress site no longer has DB writes on every page load, and your site scales better with traffic than the other options

Overall, what you’re asking for is non-trivial, and while sounding simple, will either be very performance heavy, inaccurate, or require an additional server and development time. There is a trade off.

Also, any solution you devise that runs in PHP will necessarily mean that you can no longer do full page caches unless the online offline functionality is powered by javascript. Otherwise, if you show someone as online and your pages are cached for 24 hours, they’ll still be online for the following 24 hours. If you use either of the first 2 options I presented, you’ll also be unable to use caching for logged in users, and tools such as batcache or Varnish will break your system if applied to those users unless an uncached AJAX based system is used to indicate that someone is online

Will this change in the future? No, WordPress doesn’t handle this kind of functionality or store this kind of data. WordPress does track sessions in user meta, but this is not the kind of data you are looking for. This may indicate how many devices a user has logged in on, but it’s by no means reliable, and it can’t be used to give an online or offline indicator

Leave a Comment