ip-location-block-ip-addr

Posted on May 11, 2021

The IP address of the server for the current requester.

Description

The filter hook “ip-location-block-ip-addr” assigns the IP address from which the current request comes. This plugin validate it by means of the country code and black / white list of extra IPs.

Parameters

  • $ip (string) $_SERVER['REMOTE_ADDR']

Use case #1

The following code snippet in your theme’s functions.php can replace the IP address for specific request based on the request IP. This is useful if you want to debug the plugin’s blocking functionality and ensure it is blocking correctly, you can set a blocked IP address that your blocking rule covers for your browser request and see if your access will be denied. In order for this to work you also need to log out.

Replace 88.88.88.88 with your own ip address. To check your IP, use myip.com or something else.

Replace 99.99.99.99 with IP that belongs you to the blocked/whitelisted range to test against the rule that you sent in the “Validation rules and behavior” section.

<?php
function my_replace_ip1( $ip ) {
    if('88.88.88.88' === $ip) {
       $ip = '99.99.99.99';
    }
    return $ip;
}
add_filter( 'ip-location-block-ip-addr', 'my_replace_ip1' );

Use case #2

The following code snippet in your theme’s functions.php can replace the IP address according to your browser’s user agent string. It’s useful to test this plugin’s functionality using browser’s addon which can be change the user agent string.

function my_replace_ip2( $ip ) {
    if ( FALSE !== stripos( $_SERVER['HTTP_USER_AGENT'], 'your unique string' ) )
        return '98.139.183.24'; // yahoo.com
    else
        return $ip;
}
add_filter( 'ip-location-block-ip-addr', 'my_replace_ip2' );
NOTE: It’s also useful using VPN switcher addon to fake your IP address.
NOTE: When you select "mu-plugins" (ip-location-block-mu.php) as Validation timing , you should put your code snippet into drop-in.php in Geolocation API folder instead of functions.php. See My custom functions in “functions.php” doesn’t work. in FAQ for detail.

Since

0.2.0