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

WordPress网站如何设置浏览x次页面或x分钟后需输入密码访问?

Hey there! Let's figure out how to set up that "limited free access then password prompt" system for your WordPress site. Since you haven't found a perfect plugin yet, I'll cover both plugin options and a custom code solution that gives you full control:

Plugin Recommendations

Even if you didn't spot them right away, these plugins can be tweaked to fit your needs:

  • Restricted Site Access: While it's often used for full-site restrictions, you can pair it with a session-tracking plugin (like WP Session Manager) to trigger the password prompt only after a set number of page views or time limit. Just set the plugin to "allow visitors until limit is hit" and use session data to enforce those limits.
  • Content Control: This lets you restrict content by user type, but with a bit of custom code (or using its built-in hooks), you can add session-based page view/time checks before locking content and showing a password form.
  • Simple Membership: Though it's a membership plugin, you can create a "free trial" tier that doesn't require login—visitors get limited access, then are prompted to enter a password (which you can share via your registration form) or sign up for full access.
Custom Code Solution (Flexible & Tailored)

If plugins don't cut it, this custom setup will let you build exactly what you want without extra bloat:

Step 1: Track User Sessions in functions.php

Add this code to your theme's functions.php file (or use a custom plugin to avoid losing it on theme updates):

function track_visitor_access() {
    // Start session if not already active
    if (!session_id()) {
        session_start();
    }

    // Initialize session variables on first visit
    if (!isset($_SESSION['page_views'])) {
        $_SESSION['page_views'] = 0;
        $_SESSION['access_start_time'] = time();
    }

    // Skip tracking the password verification page
    if (!is_page('password-verification')) {
        $_SESSION['page_views']++;
    }

    // Set your limits here: adjust numbers to fit your needs
    $max_page_views = 5; // Allow 5 page views
    $max_access_minutes = 30; // Allow 30 minutes of access
    $max_access_seconds = $max_access_minutes * 60;

    // Check if limits are hit and user hasn't been granted access yet
    if (!isset($_SESSION['access_granted']) && 
        ($_SESSION['page_views'] >= $max_page_views || (time() - $_SESSION['access_start_time']) >= $max_access_seconds)) {
        // Redirect to your password verification page
        wp_redirect(get_permalink(get_page_by_path('password-verification')));
        exit;
    }
}
add_action('template_redirect', 'track_visitor_access');

Step 2: Create a Password Verification Page

  1. In WordPress, create a new page named "Password Verification" (slug: password-verification).
  2. Switch to the "Code Editor" mode and paste this code:
<div class="password-prompt">
    <h2>继续访问需要验证</h2>
    <p>你已经达到了免费访问的限制,输入密码即可继续浏览,或者<a href="/register">免费注册</a>获取无限制权限!</p>

    <form method="post" action="">
        <label for="access_pass">访问密码:</label>
        <input type="password" name="access_pass" id="access_pass" required>
        <button type="submit" name="verify_pass">提交</button>
    </form>

    <?php
    // Handle password submission
    if (isset($_POST['verify_pass'])) {
        $valid_password = 'your-custom-password-here'; // Replace with your secure password
        if ($_POST['access_pass'] === $valid_password) {
            $_SESSION['access_granted'] = true;
            // Redirect back to the homepage or the last visited page
            wp_redirect(wp_get_referer() ?: home_url());
            exit;
        } else {
            echo '<p style="color: #dc3232; margin-top: 1rem;">密码错误,请重试!</p>';
        }
    }
    ?>
</div>

Step 3: Show Remaining Access to Visitors

Add this code to functions.php to display how much access time/page views visitors have left (great for encouraging registration):

function show_remaining_access() {
    if (!session_id()) {
        session_start();
    }

    if (!isset($_SESSION['access_granted'])) {
        $max_page_views = 5;
        $max_access_minutes = 30;
        $remaining_views = $max_page_views - $_SESSION['page_views'];
        $remaining_seconds = ($max_access_minutes * 60) - (time() - $_SESSION['access_start_time']);
        $remaining_minutes = floor($remaining_seconds / 60);

        echo '<div class="remaining-access" style="padding: 1rem; background: #f0f0f0; margin: 1rem 0;">
                <p>剩余免费访问:' . $remaining_views . ' 个页面 / ' . $remaining_minutes . ' 分钟</p>
                <p><a href="/register" style="color: #0073aa;">立即注册</a>解锁无限制浏览!</p>
              </div>';
    }
}
add_action('wp_footer', 'show_remaining_access');
Pro Tips
  • Make sure to use a strong password and consider rotating it periodically if needed.
  • If you want to let registered users bypass the password prompt automatically, add a check for is_user_logged_in() in the track_visitor_access function to skip restrictions.
  • Test the flow thoroughly by clearing your browser cookies/session to simulate a first-time visitor.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 15:04:08