Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active December 11, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/4583495 to your computer and use it in GitHub Desktop.
Save wokamoto/4583495 to your computer and use it in GitHub Desktop.
[WordPress] WP のユーザー認証を使った BASIC 認証
<?php
function basic_auth(){
nocache_headers();
if ( is_user_logged_in() )
return;
// WordPress のユーザー認証で BASIC 認証ユーザー/パスワードをチェック
$user = isset($_SERVER["PHP_AUTH_USER"]) ? $_SERVER["PHP_AUTH_USER"] : '';
$pwd = isset($_SERVER["PHP_AUTH_PW"]) ? $_SERVER["PHP_AUTH_PW"] : '';
if ( !is_wp_error(wp_authenticate($user, $pwd)) ) {
return;
}
// BASIC 認証が必要
header('WWW-Authenticate: Basic realm="Please Enter Your Password"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required';
die();
}
add_action('template_redirect','basic_auth');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment