2026年5月20日
2026年5月20日
WordPressのステージング環境でのログイン設定方法
はじめに
ステージング(テスト)環境では、本番環境とは異なるセキュリティ・メール設定が必要です。外部からの不正アクセス防止、検索エンジンへのインデックス防止、テストメールの誤送信防止を設定しましょう。
解決手順
ステップ1:wp-config.phpで環境を識別する
// wp-config.php — ステージング環境の定義
define('WP_ENV', 'staging');
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
// ステージング環境ではキャッシュを無効化
define('WP_CACHE', false);
ステップ2:BASIC認証でサイトを保護する
# .htaccess — ステージング環境全体をBASIC認証で保護
AuthType Basic
AuthName "Staging Environment"
AuthUserFile /home/username/.htpasswd
Require valid-user
# admin-ajax.php は除外
<Files "admin-ajax.php">
Satisfy Any
Allow from all
</Files>
ステップ3:検索エンジンのインデックスをブロックする
// functions.php — ステージングではnoindexを強制
add_action('wp_head', function() {
if (defined('WP_ENV') && WP_ENV === 'staging') {
echo '<meta name="robots" content="noindex, nofollow">' . PHP_EOL;
}
});
管理画面 → 設定 → 表示設定 → 「検索エンジンがサイトをインデックスしないようにする」にもチェックを入れてください。
ステップ4:ステージングでのメール送信を無効化または迂回する
// functions.php — ステージング環境では全メールを開発者に転送
add_filter('wp_mail', function($args) {
if (defined('WP_ENV') && WP_ENV === 'staging') {
$original_to = is_array($args['to']) ? implode(',', $args['to']) : $args['to'];
$args['to'] = 'dev@example.com';
$args['subject'] = '[STAGING → ' . $original_to . '] ' . $args['subject'];
}
return $args;
});
ステップ5:本番DBからステージングに移行後にURLを更新する
# 本番URLをステージングURLに一括置換
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables
# ユーザーのメールアドレスをテスト用に変更(誤送信防止)
wp user list --field=user_email | while read email; do
wp user update $(wp user get "$email" --field=ID) \
--user_email="$(echo $email | sed 's/@/+staging@/')"
done
# 管理者メールアドレスを変更
wp option update admin_email 'staging-admin@example.com'
ステップ6:テスト用ユーザーを作成する
# 各ロールのテストユーザーを作成
wp user create test_admin test_admin@example.com --role=administrator --user_pass='Test!Pass123'
wp user create test_editor test_editor@example.com --role=editor --user_pass='Test!Pass123'
wp user create test_author test_author@example.com --role=author --user_pass='Test!Pass123'
wp user create test_subscriber test_sub@example.com --role=subscriber --user_pass='Test!Pass123'
注意事項
- 本番データをステージングにコピーする際は、個人情報(メールアドレス・電話番号等)を匿名化することをGDPR/個人情報保護法の観点から検討してください
- ステージング環境のURLが公開されていると、SEOに影響が出る可能性があります。必ずBASIC認証またはIPアドレス制限で保護してください
まとめ
ステージング環境では「BASIC認証での保護 → noindex設定 → メール迂回 → URL置換」の4点を設定することで安全にテストができます。WP-CLIを使えばこれらをスクリプト化して自動化できます。