phpwordpressauthenticationhttp-redirectintranet

Wordpress redirect to login page if ip is different


I am making an intranet site in Wordpress that can be viewed from a specific IP or it redirects to the login page if it is accessed from another IP. I tried to redirect from .htaccess and from functions.php but with no success. In functionts.php I put this code found in another post:

    function ip_based_login() {
    $visitor = $_SERVER['REMOTE_ADDR'];

    $redirectTo = site_url('wp-admin');

    if (!preg_match("/95.81.51.134/",$visitor)) {
        wp_redirect($redirectTo);
    }

    exit;
}
add_action('init', 'ip_based_login');

This code gives me the error: The page isn’t redirecting properly in Firefox. I deleted the code form .htaccess before I thought of posting.

How can I do this?


Solution

  • Try via plugin

    Restricted Site Access

    It has what you are looking for.