Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Last active March 17, 2017 18:38
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 cklosowski/78723020a93bc030d62589bbe69f6b34 to your computer and use it in GitHub Desktop.
Save cklosowski/78723020a93bc030d62589bbe69f6b34 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Public IP FIx
* Plugin URI: https://kungfugrep.com
* Description: For testing IP sensitive code, use the public facing IP in your localhost
* Version: 1.0
* Author: Filament Studios
* Author URI: https://filament-studios.com
* License: GPL-2.0+
*/
/**
*
* The point of this plugin is to allow a local development environment to identify
* the REMOTE_ADDR of the user as the public facing IP, which is able to be used in
* GeoIP lookups and WHOIS checks for things like fraud services and other types of
* eCommerce and rate limiting checks
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function kfg_use_external_ip() {
$ip = get_transient( '_kfg_remote_ip' );
// When testing this by manually changing the IP below on line 49, uncomment the following line
// $ip = false;
if ( false === $ip ) {
$response = wp_remote_get( 'http://bot.whatismyipaddress.com/' );
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$ip = $body;
set_transient( '_kfg_remote_ip', $ip, HOUR_IN_SECONDS );
}
}
if ( false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
$_SERVER['REMOTE_ADDR'] = $ip;
}
/**
* If you want to use a specific IP Address just uncomment out the following line, and put in the IP you want to use
*/
// $_SERVER['REMOTE_ADDR'] = '123.123.123.123';
}
add_action( 'plugins_loaded', 'kfg_use_external_ip', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment