Skip Navigation

[Resolved] How to show the logged in users number?

This thread is resolved. Here is a description of the problem and solution.

Problem: I have some custom code included in a Toolset Custom Code snippet, but it doesn't work. If I move the same code into my theme's functions.php file, it works.

Solution: Most likely this is a timing issue because Toolset's custom code snippets are executed at init:20 but theme functions.php is executed earlier.

Relevant Documentation:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/#snippet-execution-timing
https://codex.wordpress.org/Plugin_API/Action_Reference

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.66 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by francescoG-3 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1182798

Tell us what you are trying to do? I need to display the number of the logged in users.

Is there any documentation that you are following? No

Is there a similar example that we can see? yes, hidden link

I have create a shortcode using the following code in Toolset Custom Code section

/**
 * Runs on each page load.
 * If the user does not exist in the array in the 'online_status' transient, then add them.
 */
function  user_online_update(){

	if ( is_user_logged_in() ) {

		// get the user activity the list
		$logged_in_users = get_transient( 'online_status' );

		// get current user ID
		$user = wp_get_current_user();

		// check if the current user needs to update his online status;
		// he does if he doesn't exist in the list
		// and if his "last activity" was less than let's say: 1 minutes ago
		$no_need_to_update = isset( $logged_in_users[$user->ID] ) && $logged_in_users[$user->ID] >  ( time() - ( 1 * 60 ) );

		// update the list if needed
		if ( ! $no_need_to_update ) {
			$logged_in_users[$user->ID] = time();
			set_transient( 'online_status', $logged_in_users, $expire_in = (  2 * 60 ) ); // 2 mins
		}
	}
}
add_action( 'wp', 'user_online_update' );

/**
 * Runs when a user logs out.
 * If the user exists in the 'online_status' array, remove them from there and update the transient.
 */
function clear_transient_on_logout() {

	$user_id = get_current_user_id();

	$users_transient_id = get_transient( 'online_status' );

	if ( is_array( $users_transient_id ) ) {
		if ( $users_transient_id[$user_id] ) {
			unset( $users_transient_id[$user_id] );
			set_transient( 'online_status', $users_transient_id, ( 2 * 60 ) ); // 2 mins
		}
	}
}
add_action( 'clear_auth_cookie', 'clear_transient_on_logout' );

/**
 * The contents of the widget
 */
function incontramici_online_users() {

	$logged_in_users = count( get_transient( 'online_status' ) ); // gets number of logged-in users
	$total_users = count_users(); // gets user info

   
	return $logged_in_users;
	
}

add_shortcode("conta-utenti-online", "incontramici_online_users");

But if I'm logged in with two different users in different browser, the counter show me always the number 1...

What is the link to your site? hidden link

#1183128

Hi, if you deactivate the custom code in Toolset > Settings > Custom Code, then move it into your child theme's functions.php file, does it work as expected?

#1183766

Hi Christian,
if I deactivate the custom code and move it into my child theme's functions.php it work fine.
Why it don't work into the Toolset Custom Code?

#1183777

Take a look at the information here:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/#snippet-execution-timing

Toolset custom codes are triggered during init:20, but code in your theme's functions.php file is triggered before init:20, during theme setup. So it's possible that the timing affects your code functionality. As a test, add this code to the end of your Toolset Code snippet:

die("This code was executed");

Remove the code from your theme's functions.php file, then activate the Toolset snippet and refresh the homepage of your site. You should see "This code was executed" appear on the page. If not, then there is a configuration issue in your code snippet and it is not being run correctly, or there is a permissions issue on your server preventing the PHP from being executed. If the text does appear, then you know the snippet code was executed and the Toolset custom code snippet feature is working as expected. At that point you know the problem is most likely related to timing.

Do you see "This code was executed" appear on the page?

#1183792

I have tried to add the code into my Toolset code snipped, run it and delete my code from my child theme functions.php.

So, it not work fine.

The code work fine only into functions.php. I think I deactivated it into the Toolset Custom Code, and run it from functions.php.

#1183800

Okay so it's a timing issue. In this case, it must go in your theme's functions.php file.

#1183809

My issue is resolved now. Thank you!