Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active October 30, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/7e6d399ecec8a6b2794ccb836e1fe800 to your computer and use it in GitHub Desktop.
Save georgestephanis/7e6d399ecec8a6b2794ccb836e1fe800 to your computer and use it in GitHub Desktop.
<?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