Validate IP Address During Authentication – For A Given User
-
I want a specific user only logging in from a specific IP-adres.
See the code below. I put it in the functions.php from my child-theme
It worked perfect before installing wpcerber.
Can some one advise me the code to work with wpcerber/**
* Validate IP Address During Authentication – For A Given User
*/
add_filter( ‘wp_authenticate’, function( $user )
{
// Adjust to your needs:
$allowed_user_ip = ‘84.86.5.72’;
$ip_restricted_user_id = 9;// Current user’s IP address
$current_user_ip = isset( $_SERVER[‘REMOTE_ADDR’] ) ? $_SERVER[‘REMOTE_ADDR’] : null;// Nothing to do for valid IP address
if( $current_user_ip === $allowed_user_ip )
return $user;// Nothing to do for users that are not IP restricted
if(
$user instanceof \WP_User
&& $user->ID > 0
&& $ip_restricted_user_id != $user->ID
)
return $user;// Add an ‘Invalid IP address’ error
if( is_wp_error( $user ) )
$user->add(
‘invalid_ip’,
sprintf(
‘%s: %s’,
esc_html__( ‘ERROR’, ‘mydomain’ ),
esc_html__( ‘IP address is invalid.’, ‘mydomain’ )
)
);
// Create a new ‘Invalid IP address’ error
else
$user = new WP_Error(
‘invalid_ip’,
sprintf(
‘%s: %s’,
esc_html__( ‘ERROR’, ‘mydomain’ ),
esc_html__( ‘IP address is invalid.’, ‘mydomain’ )
)
);return $user;
}, 1);`
The topic ‘Validate IP Address During Authentication – For A Given User’ is closed to new replies.