User online status

I’d like to suggest a feature that display if the user/vendor currently is online or when the vendor was last active.

As a bonus suggestion. Showing statistic of average response time would also be amazing

Thanks, we’ll consider adding this feature.

1 Like

Absolutely, exactly what I was thinking about as well.

2 Likes

Please this is really needed

2 Likes

Thanks for your feedback - please vote for the feature, the number of votes is one of the factors that define the feature priority. We have a long, long list of features so we have to decide which ones to implement first.

Last active would be even more useful I think!
Like, in the profile/listing i would say: “Last active 5h ago” or “Last seen on the platform 13 days ago” or something like this.

I have a solution that tracks when a user last logged on, and displays it in the user menu in the backend. It can also be shown on the front end with the shortcode.

The problem with this, and why I want a “last seen” and “currently online” instead of my solution “last logged in” is obviously that a current user stays logged in for some time, and that my solution just updates everytime the user actually logs in with a username and password, not when the user visits the website. Also the UX for customers when browsning vendors and seeing if they are active, and currently are online would be amazing, so they can engage in contant instantly.

Anyway. If you want the “last login” functionality, here it is:

//Record user's last login to custom meta
add_action('wp_login', 'smartwp_capture_login_time', 10, 2);

function smartwp_capture_login_time($user_login, $user)
{
	update_user_meta($user->ID, 'last_login', time());
}

//Register new custom column with last login time
add_filter('manage_users_columns', 'smartwp_user_last_login_column');
add_filter('manage_users_custom_column', 'smartwp_last_login_column', 10, 3);

function smartwp_user_last_login_column($columns)
{
	$columns['last_login'] = 'Last Login';
	return $columns;
}

function smartwp_last_login_column($output, $column_id, $user_id)
{
	if ($column_id == 'last_login') {
		$last_login = get_user_meta($user_id, 'last_login', true);
		$date_format = 'M j, Y';
		$hover_date_format = 'F j, Y, g:i a';

		$output = $last_login ? '<div title="Last login: ' . date($hover_date_format, $last_login) . '">' . human_time_diff($last_login) . '</div>' : 'No record';
	}

	return $output;
}

//Allow the last login columns to be sortable
add_filter('manage_users_sortable_columns', 'smartwp_sortable_last_login_column');
add_action('pre_get_users', 'smartwp_sort_last_login_column');

function smartwp_sortable_last_login_column($columns)
{
	return wp_parse_args(array(
		'last_login' => 'last_login'
	), $columns);
}

function smartwp_sort_last_login_column($query)
{
	if (!is_admin()) {
		return $query;
	}

	$screen = get_current_screen();

	if (isset($screen->base) && $screen->base !== 'users') {
		return $query;
	}

	if (isset($_GET['orderby']) && $_GET['orderby'] == 'last_login') {

		$query->query_vars['meta_key'] = 'last_login';
		$query->query_vars['orderby'] = 'meta_value';
	}

	return $query;
}

//Add [lastlogin] shortcode
function smartwp_lastlogin_shortcode($atts)
{
	$atts = shortcode_atts(
		array(
			'user_id' => false,
		),
		$atts,
		'lastlogin'
	);

	$last_login = get_the_author_meta('last_login', $atts['user_id']);
	if (empty($last_login)) {
		return false;
	};
	$the_login_date = human_time_diff($last_login);
	return $the_login_date;
}

add_shortcode('lastlogin', 'smartwp_lastlogin_shortcode');
5 Likes

Is this code something I can cut & paste with the code snippet plugin?

Yes, this works with snippet. You get a new column under users

1 Like

I copied and pasted it exactly as above. I don’t see anything new under the users tab. What did I miss?

Click the menu “users” and then, next to each username, there is a column showing when they last logged in. Probably alot of “no record” there. I can post a screenshot if you dont see it

2 Likes

Thanks I finally saw it. I had the default view settings, but in order to see the extra column, you have to switch to Classic View. I appreciate your help!

Hi all.
I think this feature is very important so I contacted a freelancer on fiverr. He was very patient, accurate and effective and now my users can see who is online both in listings, in messages and on the sellers page. In addition, on the sellers page, they can be filtered by status.
I was very satisfied, so if you need it I recommend this freelancer: https://www.fiverr.com/expert2014?source=gig_page
I ordered through this gig: https://www.fiverr.com/expert2014/develop-or-fix-custom-wordpress-plugin-or-woocommerce-plugin?source=order_page_summary_gig_link_title&funnel=9adb1b4d14f54f7d43cdc2212d539f54f7d43cdc2212d539f

3 Likes

Thanks for sharing! We plan to launch the HivePress Experts program soon and we’ll contact this freelancer if he managed to customize HivePress.

4 Likes

Super agree

1 Like

This is major. The user needs to know if the seller is active. Super important.

2 Likes

Hello,

I modified the snippet to add the shortcode. You can use the [lastlogin] shortcode. This only shows the logged-in user. Can someone modify this to show the page’s user or author. Thanks!

//Record user's last login to custom meta
add_action('wp_login', 'smartwp_capture_login_time', 10, 2);

function smartwp_capture_login_time($user_login, $user)
{
	update_user_meta($user->ID, 'last_login', time());
}

//Register new custom column with last login time
add_filter('manage_users_columns', 'smartwp_user_last_login_column');
add_filter('manage_users_custom_column', 'smartwp_last_login_column', 10, 3);

function smartwp_user_last_login_column($columns)
{
	$columns['last_login'] = 'Last Login';
	return $columns;
}

function smartwp_last_login_column($output, $column_id, $user_id)
{
	if ($column_id == 'last_login') {
		$last_login = get_user_meta($user_id, 'last_login', true);
		$date_format = 'M j, Y';
		$hover_date_format = 'F j, Y, g:i a';
		$output = $last_login ? '<div title="Last login: ' . date($hover_date_format, $last_login) . '">' . human_time_diff($last_login) . '</div>' : 'No record';
	}
	return $output;
}

//Allow the last login columns to be sortable
add_filter('manage_users_sortable_columns', 'smartwp_sortable_last_login_column');
add_action('pre_get_users', 'smartwp_sort_last_login_column');

function smartwp_sortable_last_login_column($columns)
{
	return wp_parse_args(array(
		'last_login' => 'last_login'
	), $columns);
}

function smartwp_sort_last_login_column($query)
{
	if (!is_admin()) {
		return $query;
	}
	$screen = get_current_screen();
	if (isset($screen->base) && $screen->base !== 'users') {
		return $query;
	}
	if (isset($_GET['orderby']) && $_GET['orderby'] == 'last_login') {
		$query->query_vars['meta_key'] = 'last_login';
		$query->query_vars['orderby'] = 'meta_value';
	}
	return $query;
}

//Add [lastlogin] shortcode
function smartwp_lastlogin_shortcode($atts)
{
	global $post;

	$atts = shortcode_atts(
		array(
			'user_id' => $post->post_author,  // default to the post's author ID
		),
		$atts,
		'lastlogin'
	);

	$last_login = get_the_author_meta('last_login', $atts['user_id']);
	if (empty($last_login)) {
		return false;
	};
	$the_login_date = human_time_diff($last_login);
	return $the_login_date;
}

add_shortcode('lastlogin', 'smartwp_lastlogin_shortcode');

1 Like

Hi,

Thanks for sharing!

Unfortunately, there is no simple solution for this, but we plan to add this feature soon.

Since this feature had a lot of votes, I decided to take on the challenge to get it to work. I finally got it. I reply to you because you have helped many people on here also. This new code will show the user current status (online), user last login date and time, user’s current page they are on. I also included the short codes. Thanks Seegon for being a great member of this community! Hope you all enjoy! =)

// Record user's last visit time, live status, and current page to custom meta
add_action('wp', 'smartwp_capture_visit_data');

function smartwp_capture_visit_data()
{
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $last_visit = get_user_meta($user->ID, 'last_visit', true);
        if (empty($last_visit)) {
            // If no previous visit recorded, update the last visit, set live status to true, and capture the current page
            update_user_meta($user->ID, 'last_visit', time());
            update_user_meta($user->ID, 'live_status', true);
            update_user_meta($user->ID, 'current_page', $_SERVER['REQUEST_URI']);
        } else {
            // If previous visit recorded, update the last visit, live status, and current page based on time difference
            $time_difference = time() - $last_visit;
            if ($time_difference <= 60) {
                // If the time difference is less than or equal to 60 seconds, set live status to true
                update_user_meta($user->ID, 'live_status', true);
            } else {
                // If the time difference is greater than 60 seconds, set live status to false
                update_user_meta($user->ID, 'live_status', false);
            }
            update_user_meta($user->ID, 'last_visit', time());
            update_user_meta($user->ID, 'current_page', $_SERVER['REQUEST_URI']);
        }
    }
}

// Register new custom columns for live status, last visit time, and current page
add_filter('manage_users_columns', 'smartwp_user_custom_columns');
add_filter('manage_users_custom_column', 'smartwp_custom_columns_data', 10, 3);

function smartwp_user_custom_columns($columns)
{
    $columns['live_status'] = 'Live Status';
    $columns['last_visit'] = 'Last Visit';
    $columns['current_page'] = 'Current Page';
    return $columns;
}

function smartwp_custom_columns_data($output, $column_id, $user_id)
{
    if ($column_id == 'live_status') {
        $live_status = get_user_meta($user_id, 'live_status', true);
        $output = $live_status ? 'Online' : 'Offline';
    } elseif ($column_id == 'last_visit') {
        $last_visit = get_user_meta($user_id, 'last_visit', true);
        if ($last_visit) {
            $output = date('M j, Y H:i:s', $last_visit);
        } else {
            $output = 'No record';
        }
    } elseif ($column_id == 'current_page') {
        $current_page = get_user_meta($user_id, 'current_page', true);
        $output = !empty($current_page) ? esc_html($current_page) : 'Unknown';
    }
    return $output;
}

// Allow the live status, last visit, and current page columns to be sortable
add_filter('manage_users_sortable_columns', 'smartwp_sortable_custom_columns');
add_action('pre_get_users', 'smartwp_sort_custom_columns');

function smartwp_sortable_custom_columns($columns)
{
    return wp_parse_args(array(
        'live_status' => 'live_status',
        'last_visit' => 'last_visit',
        'current_page' => 'current_page'
    ), $columns);
}

function smartwp_sort_custom_columns($query)
{
    if (!is_admin()) {
        return $query;
    }
    $screen = get_current_screen();
    if (isset($screen->base) && $screen->base !== 'users') {
        return $query;
    }
    if (isset($_GET['orderby'])) {
        $orderby = $_GET['orderby'];
        if ($orderby == 'live_status' || $orderby == 'last_visit' || $orderby == 'current_page') {
            $query->query_vars['meta_key'] = $orderby;
            $query->query_vars['orderby'] = 'meta_value';
        }
    }
    return $query;
}

// Add [livestatus], [lastvisit], and [currentpage] shortcodes
function smartwp_livestatus_shortcode($atts)
{
    $atts = shortcode_atts(
        array(
            'user_id' => get_current_user_id(),
        ),
        $atts,
        'livestatus'
    );

    $live_status = get_user_meta($atts['user_id'], 'live_status', true);
    return $live_status ? 'Online' : 'Offline';
}

function smartwp_lastvisit_shortcode($atts)
{
    $atts = shortcode_atts(
        array(
            'user_id' => get_current_user_id(),
        ),
        $atts,
        'lastvisit'
    );

    $last_visit = get_user_meta($atts['user_id'], 'last_visit', true);
    if ($last_visit) {
        return date('M j, Y H:i:s', $last_visit);
    } else {
        return 'No record';
    }
}

function smartwp_currentpage_shortcode($atts)
{
    $atts = shortcode_atts(
        array(
            'user_id' => get_current_user_id(),
        ),
        $atts,
        'currentpage'
    );

    $current_page = get_user_meta($atts['user_id'], 'current_page', true);
    return !empty($current_page) ? esc_html($current_page) : 'Unknown';
}

add_shortcode('livestatus', 'smartwp_livestatus_shortcode');
add_shortcode('lastvisit', 'smartwp_lastvisit_shortcode');
add_shortcode('currentpage', 'smartwp_currentpage_shortcode');


11 Likes

Thanks for sharing, this is great!

1 Like