You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将wp-login.php设为首页后,如何开放指定WordPress页面访问权限?

解决方案

情况1:使用Password Protect WordPress(PPWP)插件的全站保护功能

你之前的代码不完整,缺少核心的页面判断逻辑和返回值,所以无法生效。替换成以下完整代码:

add_filter('ppwp_sitewide_has_bypass', function ($allowed) {
    if ($allowed) {
        return $allowed;
    }

    // 要开放的页面slug列表
    $whitelist_pages = ['samples'];
    // 获取当前页面的slug
    global $post;
    $current_slug = isset($post->post_name) ? $post->post_name : '';

    // 判断当前页面是否在白名单内
    if (in_array($current_slug, $whitelist_pages)) {
        return true;
    }

    return $allowed;
});

这段代码会检查当前访问的页面slug是否在白名单里,如果是则允许跳过全站登录验证。

情况2:手动编写全局重定向代码(未使用PPWP插件)

如果你是直接在functions.php里写了重定向到wp-login.php的代码,需要修改重定向逻辑,排除指定页面。比如原来的代码可能类似这样:

function redirect_to_login() {
    if (!is_user_logged_in()) {
        wp_redirect(wp_login_url());
        exit;
    }
}
add_action('template_redirect', 'redirect_to_login');

修改后加入白名单判断:

function redirect_to_login() {
    // 要开放的页面slug或ID列表
    $whitelist_pages = ['samples', 'another-sample'];
    // 获取当前页面的slug和ID
    global $post;
    $current_slug = isset($post->post_name) ? $post->post_name : '';
    $current_id = isset($post->ID) ? $post->ID : '';

    // 如果当前页面在白名单内,或者用户已登录,不执行重定向
    if (is_user_logged_in() || in_array($current_slug, $whitelist_pages) || in_array($current_id, $whitelist_pages)) {
        return;
    }

    wp_redirect(wp_login_url());
    exit;
}
add_action('template_redirect', 'redirect_to_login');

你可以根据实际需求,用页面slug或ID来指定开放的页面。

注意事项

  • 修改functions.php前建议先备份文件,避免出错导致网站无法访问
  • 确保页面slug填写正确,可以在WordPress后台的页面编辑页查看页面slug
  • 如果是静态页面或自定义路由,可能需要调整判断逻辑,比如用$_SERVER['REQUEST_URI']来匹配URL路径

内容的提问来源于stack exchange,提问作者Digi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 22:35:20