Skip to Content
Menu

online_users()

Get a count of online users or an array of online user IDs.

PHP March 16, 2017

Usage

PHP
nebula()->online_users($count)

Parameters

$return
(Optional) (String) What data to return
Default: count

Parameter Notes

“count” returns a total count, otherwise this returns an array of all online user IDs.

Request or provide clarification »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Admin/Users.php on line 202.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_online_users"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function online_users($return='count'){
                $override = apply_filters('pre_nebula_online_users', null, $return);
                if ( isset($override) ){return;}
    
                $logged_in_users = $this->get_data('users_status');
                if ( empty($logged_in_users) || !is_array($logged_in_users) ){
                    return ( strtolower($return) === 'count' )? 0 : false; //If this happens it indicates an error.
                }
    
                $user_online_count = 0;
                $online_users = array();
                foreach ( $logged_in_users as $user ){
                    if ( !empty($user['username']) && isset($user['last']) && $user['last'] > time()-600 ){
                        $online_users[] = $user;
                        $user_online_count++;
                    }
                }
    
                return ( strtolower($return) === 'count' )? $user_online_count : $online_users;
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_online_users', 'my_custom_online_users', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_online_users($null, $return){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_online_users', '__return_false');