WordPress – setting basic HTTP authentication on front end

I want to implement basic HTTP authentication on my WordPress site. I tried with plugins and I have also tried to use my custom code inside of header.php:

    <?php
  $username = 'admin';
  $password = 'letmein';

  if (isset($_SERVER['PHP_AUTH_USER']) &&
      isset($_SERVER['PHP_AUTH_PW']))
  {
    if ($_SERVER['PHP_AUTH_USER'] == $username &&
        $_SERVER['PHP_AUTH_PW']   == $password)
          echo "You are now logged in";
    else die("Invalid username / password combination");
  }
  else
  {
    header('WWW-Authenticate: Basic realm="Restricted Section"');
    header('HTTP/1.0 401 Unauthorized');
    die ("Please enter your username and password");
  }
?>

Please, let me know how I can implement this into a wordpress in order to protect front end of my site.

Thanks!

Read more here: WordPress – setting basic HTTP authentication on front end

Leave a Reply

Your email address will not be published. Required fields are marked *