Title: 2 Adminstrators
Last modified: January 31, 2024

---

# 2 Adminstrators

 *  Resolved [schweg](https://wordpress.org/support/users/schweg/)
 * (@schweg)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/)
 * Hello everyone
   I have a question here?
 * I have created two administrators in my WordPress installation.
 * each of these users logs in on a different pc.
 * Is it possible to see in wordepress if both users are logged in?
 * Thank you
   kind regardsFranz

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17388598)
 * This plugin makes something like this possible: [https://wordpress.org/plugins/wp-security-audit-log/](https://wordpress.org/plugins/wp-security-audit-log/)
 *  [luk4](https://wordpress.org/support/users/luk4/)
 * (@luk4)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17388715)
 * Hi [@schweg](https://wordpress.org/support/users/schweg/)
 * Or maybe this one, but I haven’t tried: [https://wordpress.org/plugins/admin-users-logged-in/](https://wordpress.org/plugins/admin-users-logged-in/)
 *  [isodos](https://wordpress.org/support/users/isodos/)
 * (@isodos)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17388946)
 * Add this snippet to your theme’s `functions.php` file.
    - A check to ensure only administrators’ logins and logouts are tracked and 
      stored.
    - An admin page that displays login history, specifically for administrators,
      with access restricted to users who have the ‘manage_options’ capability, 
      which typically includes administrators.
    - A filter in the display function to ensure it shows activities for the last
      7 days.
 *     ```wp-block-code
       // Track admin user login and logout with history
       add_action('wp_login', function ($user_login, $user) {
           // Check if the user has the administrator role
           if (in_array('administrator', (array) $user->roles)) {
               $current_time = time();
               $logged_in_users = get_option('logged_in_admins_history') ?: array();
               $logged_in_users[$user->ID][] = ['login' => $current_time];
               update_option('logged_in_admins_history', $logged_in_users);
           }
       }, 10, 2);
   
       add_action('wp_logout', function ($user_id) {
           $user = get_user_by('id', $user_id);
           // Check if the user has the administrator role
           if (in_array('administrator', (array) $user->roles)) {
               $current_time = time();
               $logged_in_users = get_option('logged_in_admins_history') ?: array();
               if (isset($logged_in_users[$user_id]) && end($logged_in_users[$user_id])['login']) {
                   $logged_in_users[$user_id][] = ['logout' => $current_time];
               }
               update_option('logged_in_admins_history', $logged_in_users);
           }
       });
   
       // Add a page to the admin menu
       add_action('admin_menu', function() {
           add_management_page(
               'Administrator Login History', // Page title
               'Admin Login History', // Menu title
               'manage_options', // Capability
               'admin-login-history', // Menu slug
               'render_admin_login_history' // Callback function
           );
       });
   
       // Callback function to display login history
       function render_admin_login_history() {
           if (!current_user_can('manage_options')) {
               wp_die('You do not have sufficient permissions to access this page.');
           }
   
           $logged_in_users_history = get_option('logged_in_admins_history') ?: array();
           echo '<div class="wrap"><h1>Administrator Login History (Last 7 Days)</h1>';
           foreach ($logged_in_users_history as $user_id => $login_records) {
               $user_info = get_userdata($user_id);
               // Display only if user is an administrator
               if (in_array('administrator', (array) $user_info->roles)) {
                   echo '<h2>' . esc_html($user_info->user_login) . '</h2>';
                   echo '<ul>';
                   foreach ($login_records as $record) {
                       foreach ($record as $action => $timestamp) {
                           // Show records for the last 7 days only
                           if ($timestamp > time() - 7 * DAY_IN_SECONDS) {
                               echo '<li>' . esc_html($action) . ': ' . esc_html(date('Y-m-d H:i:s', $timestamp)) . '</li>';
                           }
                       }
                   }
                   echo '</ul>';
               }
           }
           echo '</div>';
       }
       ```
   
 *  Thread Starter [schweg](https://wordpress.org/support/users/schweg/)
 * (@schweg)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17391874)
 * Hello everyone
   Thank you very much for your answers.Sorry, I think I was misunderstood.
 * Maybe you understand me better this way.
 * There are 2 users with admin rights on this Word Press installation
   are present.
 * I just want to know if I can see when I log in whether the other admin is already
   logged in?
 * Thank you
   kind regardsFranz
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17391936)
 * This would be feasible with all 3 options mentioned. Decide which one works best
   for you.
 *  [isodos](https://wordpress.org/support/users/isodos/)
 * (@isodos)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17392056)
 * I see. You want a message advising you that another administrator is logged in
   when you login…
 * _Note that this solution involves custom session management to track user logins,
   as WordPress doesn’t natively track real-time sessions in the database._
 *     ```wp-block-code
       function custom_track_admin_login( $user_login, $user ) {
           if ( in_array( 'administrator', (array) $user->roles ) ) {
               // Set a transient to expire after a certain period, e.g., 1 hour (3600 seconds).
               set_transient( 'admin_logged_in_' . $user->ID, time(), 3600 );
           }
       }
       add_action('wp_login', 'custom_track_admin_login', 10, 2);
   
       function custom_track_admin_logout( $user_id ) {
           // Delete the transient on logout.
           delete_transient( 'admin_logged_in_' . $user_id );
       }
       add_action('wp_logout', 'custom_track_admin_logout');
   
       function custom_check_other_admin_logged_in() {
           if (current_user_can('administrator')) {
               $current_user_id = get_current_user_id();
               $admins = get_users(array('role' => 'administrator', 'exclude' => array($current_user_id)));
   
               foreach ($admins as $admin) {
                   if ( get_transient( 'admin_logged_in_' . $admin->ID ) ) {
                       // Since we're inserting this into a script, ensure it's properly escaped.
                       $alert_message = esc_js('Another admin is currently logged in.');
                       echo "<script>alert('{$alert_message}');</script>";
                       break; // Exit the loop after finding the first logged-in admin.
                   }
               }
           }
       }
       add_action('admin_notices', 'custom_check_other_admin_logged_in');
       ```
   
 *  Thread Starter [schweg](https://wordpress.org/support/users/schweg/)
 * (@schweg)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17392462)
 * Thank you very much
   It works great like this
 * Super support
 * kind regards Franz
 *  [isodos](https://wordpress.org/support/users/isodos/)
 * (@isodos)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17392856)
 * You’re welcome. Mark the thread as resolved if your question has been answered.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘2 Adminstrators’ is closed to new replies.

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 10 replies
 * 4 participants
 * Last reply from: [isodos](https://wordpress.org/support/users/isodos/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/2-adminstrators/#post-17392856)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
