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
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?
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?
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?
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.
Okay so it's a timing issue. In this case, it must go in your theme's functions.php file.
My issue is resolved now. Thank you!