Return to Snippet

Revision: 69382
at June 9, 2015 19:22 by nbpurohit


Updated Code
// Allow login from specific IP Address
function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return apply_filters( 'wpb_get_ip', $ip );
}

add_shortcode('show_ip', 'get_the_user_ip');

add_filter( 'authenticate', 'my_custom_authenticate', 10, 3 );
function my_custom_authenticate(){
    //Get POSTED value
	if(isset($_POST['log']) && !empty($_POST['log'])){
	
	$username='';
    $username=$_POST['log'];
	$admin_ip='000.000.00.00'; // replace your IP here
	$user_id=2;
	$current_ip=do_shortcode('[show_ip]');
    //Get user object
   	$user = get_user_by('login',$username);
	  
	if($user_id==$user->ID){
	    if($admin_ip != $current_ip){
			add_filter('login_errors','login_error_message');
	
			function login_error_message($error){
				
			    //check if that's the error you are looking for
			    $pos = strpos($error, 'incorrect');
			    if (is_int($pos)) {
			        //its the right error so you can overwrite it
			        $error = "<strong>ERROR</strong>: You're unique identifier was invalid.";
			    }
			    return $error;
			}
	        //User note found, or no value entered or doesn't match stored value - don't proceed.
	            remove_action('authenticate', 'wp_authenticate_username_password', 20); 
	
	    }
    }

    //Make sure you return null 
    return null;
    }
}

Revision: 69381
at June 9, 2015 19:19 by nbpurohit


Initial Code
// Allow login from specific IP Address
function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return apply_filters( 'wpb_get_ip', $ip );
}

add_shortcode('show_ip', 'get_the_user_ip');

add_filter( 'authenticate', 'my_custom_authenticate', 10, 3 );
function my_custom_authenticate(){
    //Get POSTED value
	if(isset($_POST['log']) && !empty($_POST['log'])){
	
	$username='';
    $username=$_POST['log'];
	$admin_ip='192.168.3.22';
	$user_id=2;
	$current_ip=do_shortcode('[show_ip]');
    //Get user object
   	$user = get_user_by('login',$username);
	  
	if($user_id==$user->ID){
	    if($admin_ip != $current_ip){
			add_filter('login_errors','login_error_message');
	
			function login_error_message($error){
				
			    //check if that's the error you are looking for
			    $pos = strpos($error, 'incorrect');
			    if (is_int($pos)) {
			        //its the right error so you can overwrite it
			        $error = "<strong>ERROR</strong>: You're unique identifier was invalid.";
			    }
			    return $error;
			}
	        //User note found, or no value entered or doesn't match stored value - don't proceed.
	            remove_action('authenticate', 'wp_authenticate_username_password', 20); 
	
	    }
    }

    //Make sure you return null 
    return null;
    }
}

Initial URL


Initial Description
Allow login from specific IP Address

Initial Title
Allow login from specific IP Address

Initial Tags
wordpress

Initial Language
PHP