Skip to content

Instantly share code, notes, and snippets.

@JPry
Last active December 10, 2015 01:08
Show Gist options
  • Save JPry/4355840 to your computer and use it in GitHub Desktop.
Save JPry/4355840 to your computer and use it in GitHub Desktop.

Whitelist IPs

Add whitelisted IPs to the Limit Login Attempts plugin.There are 3 different versions: One with a simple array list, the second uses a loop to build up an array instead of hand-coding every IP address in the list, and the third allows for a CIDR range. Adjust to your own needs.

Usage

Coming soon...

<?php
/**
* Plugin Name: Whitelist IPs
* Plugin URI: https://gist.github.com/JPry/4355840#file-whitelist-ips1-php
* Description: Whitelists a specific set of IP addresses for the Limit Login Attempts plugin.
* Version: 1.0
* Author: Jeremy Pry <[email protected]>
* Author URI: http://jeremypry.com
* License GPL3
*/
add_filter( 'limit_login_whitelist_ip', 'jpry_whitelist_ip', 10, 2 );
/**
* Ensure that certain IP addresses are whitelisted
*
* This function compares the current IP address against a list of IPs to whitelist. If the current
* IP is found among them, then it returns true
*
* @author Jeremy Pry <[email protected]>
* @param boolean $allow Whether the current IP is allowed
* @param string $ip The current IP address
* @return boolean True if IP is in the whitelist, otherwise the default <var>$allow</var> value
*/
function jpry_whitelist_ip( $allow, $ip ) {
// Only process if the IP isn't already allowed
if ( ! $allow ) {
// Replace the list below with your own IP address(es)
$allowed = array(
);
if ( in_array( $ip, $allowed ) ) {
$allow = true;
}
}
return $allow;
}
<?php
/**
* Plugin Name: Whitelist IPs
* Plugin URI: https://gist.github.com/JPry/4355840#file-whitelist-ips2-php
* Description: Whitelists a specific set of IP addresses for the Limit Login Attempts plugin.
* Version: 1.0
* Author: Jeremy Pry <[email protected]>
* Author URI: http://jeremypry.com
* License GPL3
*/
add_filter( 'limit_login_whitelist_ip', 'jpry_whitelist_ip', 10, 2 );
/**
* Ensure that certain IP addresses are whitelisted
*
* This function compares the current IP address against a list of IPs to whitelist. If the current
* IP is found among them, then it returns true
*
* @author Jeremy Pry <[email protected]>
* @param boolean $allow Whether the current IP is allowed
* @param string $ip The current IP address
* @return boolean True if IP is in the whitelist, otherwise the default <var>$allow</var> value
*/
function jpry_whitelist_ip( $allow, $ip ) {
// Only process if the IP isn't already allowed
if ( ! $allow ) {
$allowed = array();
// Use a loop to build a larger range of IPs
for ( $i = 209; $i <= 223; $i++ ) {
$allowed[] = '69.95.71.' . $i;
}
if ( in_array( $ip, $allowed ) ) {
$allow = true;
}
}
return $allow;
}
<?php
/**
* Plugin Name: Whitelist IPs
* Plugin URI: https://gist.github.com/JPry/4355840#file-whitelist-ips3-php
* Description: Whitelists a specific set of IP addresses for the Limit Login Attempts plugin.
* Version: 1.0
* Author: Jeremy Pry <[email protected]>
* Author URI: http://jeremypry.com
* License GPL3
*/
add_filter( 'limit_login_whitelist_ip', 'jpry_whitelist_ip', 10, 2 );
/**
* Ensure that certain IP addresses are whitelisted
*
* This function compares the current IP address against a list of IPs to whitelist. If the current
* IP is found among them, then it returns true
*
* Uses code from <link>http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5</link>
*
* @author Jeremy Pry <[email protected]>
* @param boolean $allow Whether the current IP is allowed
* @param string $ip The current IP address
* @return boolean True if IP is in the whitelist, otherwise the default <var>$allow</var> value
*/
function jpry_whitelist_ip( $allow, $ip ) {
// Only process if the IP isn't already allowed
if ( ! $allow ) {
$range = '204.69.64.0/18';
// Break apart the range
list ( $subnet, $bits ) = explode( '/', $range );
// Set up the IP and subnet for checking
$ip = ip2long( $ip );
$subnet = ip2long( $subnet );
// Bitwise check to see if the IP falls within the subnet
$mask = -1 << ( 32 - $bits );
$subnet &= $mask;
// Final check for whether to allow the IP
if ( ( $ip & $mask ) == $subnet ) {
$allow = true;
}
}
return $allow;
}
@JPry
Copy link
Author

JPry commented Jan 18, 2013

You should copy this entire file into the wp-content/mu-plugins/ directory, where it will be auto-loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment