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 regards
Franz
-
This plugin makes something like this possible: https://ww.wp.xz.cn/plugins/wp-security-audit-log/
Hi @schweg
Or maybe this one, but I haven’t tried: https://ww.wp.xz.cn/plugins/admin-users-logged-in/
Add this snippet to your theme’s
functions.phpfile.- 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.
// 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>'; }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 regards
FranzThis would be feasible with all 3 options mentioned. Decide which one works best for you.
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.
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');Thank you very much
It works great like thisSuper support
kind regards Franz
You’re welcome. Mark the thread as resolved if your question has been answered.
The topic ‘2 Adminstrators’ is closed to new replies.