カスタム投稿タイプのアーカイブにベーシック認証をかける
//Basic認証を掛けるための関数を記述
function basic_auth($auth_list,$realm="Restricted Area",$failed_text="認証に失敗しました"){
if (isset($_SERVER['PHP_AUTH_USER']) and isset($auth_list[$_SERVER['PHP_AUTH_USER']])){
if ($auth_list[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']){
return $_SERVER['PHP_AUTH_USER'];
}
}
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.0 401 Unauthorized');
header('Content-type: text/html; charset='.mb_internal_encoding());
die($failed_text);
}
<?php
if(!is_home()):
if(is_post_type_archive('limited')||is_singular('limited')):
$userArray = array("admin" => "password"
);
basic_auth($userArray);
endif;
endif;?>
cgi版PHPなどでは上記が動かないことがあるので、その時は↓
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
( ..)φメモメモ
サイト全体にベーシック認証がかかっているテストサイトでは、ベーシック認証二重掛けとなってしまってうまくいかないことも。
サーバーのIPアドレスで分岐する方法もあると思うが、
そのフォルダ(今回ならテストの/limited/)のフォルダに
Satisfy any
order allow,deny
allow from all
これで403が出るようなら同フォルダに
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/../wp/wp-blog-header.php' );
↑これは、/wp/フォルダにwordpressが入っていて、ページURL上は/wp/が消されている場合
おまけ:特定のタクソノミーの記事にだけパスワードをかけたいとき
if(is_tax('limit')||has_term('limit','limited_cat')):
$userArray = array("admin" => "password"
);