/ Published in: PHP
Allow login from specific IP Address
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Allow login from specific IP Address function get_the_user_ip() { //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; //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 $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 //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; } }