// ==============================
// カスタムログインURL制御
// ==============================
add_action('init', function () {
$request_uri = trim($_SERVER['REQUEST_URI'], '/');
// 管理者専用ログインURL
if ($request_uri === 'Ta7JN-dQNXmSp*T,tuJpWVECxAwBQGpKV8Ba_XN') {
show_custom_login_form('admin');
exit;
}
// スタッフ専用ログインURL
if ($request_uri === 'aXfBFtAz_MEyWbedt,Mn.ZDunB.*vr3tf,w5QUm') {
show_custom_login_form('staff');
exit;
}
});
// ログインフォーム表示関数(ロール別)
function show_custom_login_form($role) {
if (is_user_logged_in()) {
wp_redirect(home_url());
exit;
}
// 管理者用
$redirect = ($role === 'admin') ? admin_url() : home_url('/myaccount'); // デフォルトはマイアカウントページ
// 任意のロゴ画像URL(テーマ内に設置しても可)
$logo_url = esc_url(get_stylesheet_directory_uri() . '/image/logo.svg');
echo '
';
echo 'ログイン';
echo '';
echo '';
echo '';
echo '
';
echo '
ログイン(' . esc_html($role) . ')
';
wp_login_form(['redirect' => $redirect]);
echo '
';
}
// ==============================
// ログイン後のリダイレクト制御
// ==============================
add_filter('login_redirect', function ($redirect_to, $request, $user) {
if (is_wp_error($user)) return $redirect_to;
$roles = (array) $user->roles;
// 管理者の場合
if (in_array('administrator', $roles)) {
return admin_url(); // 管理者は管理画面にリダイレクト
}
// スタッフの場合(管理者URLからスタッフがログインした場合も含む)
elseif (array_intersect($roles, ['editor', 'author', 'shop_manager'])) {
return home_url('/myaccount'); // スタッフはマイアカウントページへ
}
return $redirect_to; // その他のユーザーはデフォルトのリダイレクト
}, 10, 3);
// ==============================
// wp-login.php へのアクセス制限(セキュリティ対策)
// ==============================
add_action('login_init', function () {
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
return;
}
if (is_user_logged_in()) {
if (!current_user_can('administrator') && !current_user_can('editor') && !current_user_can('shop_manager')) {
wp_die('このページにはアクセスできません。');
}
}
});
// ==============================
// ログアウト後のリダイレクト制御
// ==============================
add_filter('logout_redirect', function($redirect_to, $requested_redirect_to, $user) {
// WooCommerceなどで明示的にリダイレクト先が指定されている場合はそちらを優先
if (!empty($requested_redirect_to)) {
return $requested_redirect_to;
}
// ログアウト後はトップページにリダイレクト
return home_url('/');
}, 10, 3);
// ==============================
// 認証時のユーザー権限チェック
// ==============================
add_filter('authenticate', function($user, $username, $password) {
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
// 管理者専用ログインURL
if (strpos($uri, 'Ta7JN-dQNXmSp') !== false) {
if (!username_has_role($username, ['administrator'])) {
return new WP_Error('access_denied', 'このURLからはログインできません(管理者専用)');
}
}
// スタッフ専用ログインURL
if (strpos($uri, 'aXfBFtAz_MEyWbedt') !== false) {
if (!username_has_role($username, ['editor', 'author', 'shop_manager'])) {
return new WP_Error('access_denied', 'このURLからはログインできません(スタッフ専用)');
}
}
}
return $user;
}, 20, 3);
// ユーザー名からロールをチェックする関数
function username_has_role($username, $roles = []) {
$user = get_user_by('login', $username);
if (!$user) return false;
return array_intersect((array) $user->roles, $roles);
}