Last active
February 2, 2024 17:22
-
-
Save afragen/dda81b07997c5cc1d58fd90396adeb0a to your computer and use it in GitHub Desktop.
Log user IP to debug.log on login.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Log User IP. | |
* | |
* @package Fragen\Log_User_IP | |
* | |
* Plugin Name: Log User IP | |
* Plugin URI: https://gist.github.com/afragen/dda81b07997c5cc1d58fd90396adeb0a | |
* Description: Log user IP to debug.log at login. | |
* Version: 0.1.0 | |
* Author: Andy Fragen | |
* License: MIT | |
* Requires at least: 5.2 | |
* Requires PHP: 7.1 | |
* Gist Plugin URI: https://gist.github.com/afragen/dda81b07997c5cc1d58fd90396adeb0a | |
*/ | |
namespace Fragen; | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( | |
'login_redirect', | |
function ( $redirect_to, $requested_redirect_to, $user ) { | |
if ( $user instanceof \WP_User && $user->exists() ) { | |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
error_log( $user->data->user_login . ' is logged in from IP ' . $ip ); | |
} | |
return $redirect_to; | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment