wordpresshookwordpress-login

how to force login via a custom login to restrict visitors from viewing a wordpress website?


I have built a custom login page to replace the default WordPress Login Page via a custom plugin - Referencing website: https://code.tutsplus.com/tutorials/build-a-custom-wordpress-user-flow-part-1-replace-the-login-page--cms-23627

This is an awesome plugin tutorial, as it does what I need for a client's website, however, now I am trying to find a solution that forces website visitors to be redirected to the newly created custom login page to prevent anyone from seeing any another pages/posts on the site without first logging in. I hope that this makes sense.

I have tried many of the WordPress' force login plugins without success, as these create errors of too many redirects issues.

Can anyone solve this??

Thank you in advance....


Solution

  • Hi hope you already created a custom login page, next please place below code to your themes function.php file

    function admin_redirect() {
    if ( !is_user_logged_in()) {
       wp_redirect( home_url('custome page slug') );
       exit;
    }
    }
    add_action('get_header', 'admin_redirect');
    

    Above code will check if a user is logged in or not, if not logged in then will redirect to given url.