Skip to content

Instantly share code, notes, and snippets.

@ohid
Created May 17, 2021 16:08
Show Gist options
  • Save ohid/ffdfed265b353163b9f2fd91e1a27120 to your computer and use it in GitHub Desktop.
Save ohid/ffdfed265b353163b9f2fd91e1a27120 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Defender Pro] - Fix Current User IP
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin is specially integrated with the Defender Pro plugin that sets the current user IP
* Task: SLS-1326
* Version: 1.0.0
* Author: Ohid @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_filter('defender_user_ip', function() {
$headers = [
'HTTP_CLIENT_IP',
'HTTP_X_REAL_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR',
];
foreach ( $headers as $key ) {
if ( array_key_exists( $key, $_SERVER ) === true ) {
// Check if the variable is not an array
if ( ! is_array( $_SERVER[ $key ] ) ) {
return $_SERVER[ $key ];
}
$ip_array = explode( ',', $_SERVER[ $key ] );
$the_ip = reset($ip_array);
if ( $the_ip !== false ) {
return $the_ip;
}
// Break the loop once we have found the IP
break;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment