Skip to content

Instantly share code, notes, and snippets.

@ashleyfae
Created September 4, 2015 13:57
Show Gist options
  • Save ashleyfae/a371665e4ed27ebb910d to your computer and use it in GitHub Desktop.
Save ashleyfae/a371665e4ed27ebb910d to your computer and use it in GitHub Desktop.
Emails the site administrator whenever a user logs in.
/**
* Track User IP
*
* Emails the site administrator with the logged in user's IP
* address. Triggers whenever a person logs in.
*
* @param string $user_login Username of logged in user
* @param WP_User $user User object
*
* @return void
*/
function ng_track_user_ip( $user_login, $user ) {
$ip = ng_get_ip();
$admin_email = get_option( 'admin_email' );
$message = sprintf( __( '%1$s logged in from IP address: %2$s' ), $user_login, $ip );
wp_mail( $admin_email, __( 'New Site Login' ), $message );
}
add_action( 'wp_login', 'ng_track_user_ip', 10, 2 );
/**
* Get IP Address
*
* Gets the IP address of the current user.
*
* @return string
*/
function ng_get_ip() {
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'];
}
return $ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment