Skip to content

Instantly share code, notes, and snippets.

@kierzniak
Last active May 25, 2018 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kierzniak/af7fd4013a4b0275eb0596587ea9d3e1 to your computer and use it in GitHub Desktop.
Save kierzniak/af7fd4013a4b0275eb0596587ea9d3e1 to your computer and use it in GitHub Desktop.
Require basic authentication for all pages except home page
<?php
/**
* Require basic authentication for all pages except home page
*
* @return void
*/
function motivast_require_auth() {
define('AUTH_USER', 'admin');
define('AUTH_PASS', 'admin');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$is_authenticated = (
!empty( $_SERVER['PHP_AUTH_USER'] ) && !empty( $_SERVER['PHP_AUTH_PW'] ) &&
$_SERVER['PHP_AUTH_USER'] == AUTH_USER &&
$_SERVER['PHP_AUTH_PW'] == AUTH_PASS
);
if ( !is_front_page() && !$is_authenticated ) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
exit;
}
}
add_action('wp', 'motivast_require_auth');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment