Last active
October 30, 2020 16:51
-
-
Save georgestephanis/7e6d399ecec8a6b2794ccb836e1fe800 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name: Extra User Registration Source Tracking | |
* Description: Store data about a user's origin -- IP Address and User Agent -- when registering. | |
* Author: georgestephanis | |
* Author URI: http://wpspecialprojects.wordpress.com/ | |
* License: GPLv2+ | |
*/ | |
add_action( 'user_register', 'to51_track_source_user_register' ); | |
function to51_track_source_user_register( $user_id ) { | |
$source_data = array( | |
'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'], | |
'USER_AGENT' => $_SERVER['HTTP_USER_AGENT'], | |
); | |
// In order of preference, with the best ones for this purpose first. | |
$address_headers = array( | |
'HTTP_CLIENT_IP', | |
'HTTP_X_FORWARDED_FOR', | |
'HTTP_X_FORWARDED', | |
'HTTP_X_CLUSTER_CLIENT_IP', | |
'HTTP_FORWARDED_FOR', | |
'HTTP_FORWARDED', | |
); | |
foreach ( $address_headers as $header ) { | |
if ( array_key_exists( $header, $_SERVER ) ) { | |
$source_data[ $header ] = $_SERVER[ $header ]; | |
} | |
} | |
add_user_meta( $user_id, '_registration_source_data', $source_data, true ); | |
wp_mail( | |
get_bloginfo( 'admin_email' ), | |
sprintf( | |
'Registration from %1$s on "%2$s" for UID %3$d', | |
$source_data['REMOTE_ADDR'], | |
get_bloginfo( 'name' ), | |
$user_id | |
), | |
wp_json_encode( $source_data, JSON_PRETTY_PRINT ) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment