Last active
May 25, 2018 15:15
-
-
Save kierzniak/af7fd4013a4b0275eb0596587ea9d3e1 to your computer and use it in GitHub Desktop.
Require basic authentication for all pages except home page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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