如何在WordPress网站中限制特定页面并实现登录注册后跳转至该页面
Hey there! I’ve tackled this exact requirement multiple times for WordPress sites, so let’s break down the solutions that’ll get you what you need—showing login/register forms on restricted pages and redirecting users straight back to that page after they sign up or log in.
Most popular page restriction plugins (free or premium) already have this functionality built in—you just need to dig into the settings:
- For premium plugins (like Restrict Content Pro, MemberPress, or Paid Memberships Pro):
- Navigate to the plugin’s restriction rules or form settings.
- Look for an option labeled Redirect After Login or Redirect After Registration.
- Select the "Current Page" option, or use a dynamic placeholder like
{{current_url}}or%requested_url%(the exact syntax varies by plugin).
- For free plugins (like WP Access Control or Restricted Site Access):
- Check the plugin’s "Login Settings" tab. Many let you set the redirect URL to the page the user was trying to access before being restricted.
If your plugin doesn’t have this option, don’t worry—we can build it with custom code.
This approach works with any setup, even if you’re not using a restriction plugin (just hiding content for non-logged-in users).
Step 1: Add Redirect Hooks to Your Theme’s functions.php
Add these code snippets to your child theme’s functions.php file (or use a custom code plugin like Code Snippets to avoid losing changes when updating your theme):
Redirect After Login
add_filter('login_redirect', 'custom_login_redirect', 10, 3); function custom_login_redirect($redirect_to, $request, $user) { // Prioritize the redirect_to parameter if it's set and not pointing to "My Account" if (isset($_REQUEST['redirect_to']) && !empty($_REQUEST['redirect_to']) && strpos($_REQUEST['redirect_to'], 'my-account') === false) { return esc_url($_REQUEST['redirect_to']); } // Fallback to the page the user came from (if it's not the login page itself) $referrer = wp_get_referer(); if ($referrer && strpos($referrer, 'wp-login.php') === false) { return esc_url($referrer); } // Default: keep the original redirect if none of the above apply return $redirect_to; }
Redirect After Registration
add_filter('registration_redirect', 'custom_registration_redirect'); function custom_registration_redirect($redirect_to) { // Send users back to the page they registered from $referrer = wp_get_referer(); if ($referrer && strpos($referrer, 'wp-login.php') === false) { return esc_url($referrer); } return $redirect_to; }
Step 2: Add a Custom Login/Register Form Shortcode
Create a shortcode you can embed on any restricted page to show the login/register forms with the correct redirect parameter:
add_shortcode('restricted_access_form', 'restricted_access_form_shortcode'); function restricted_access_form_shortcode() { ob_start(); if (is_user_logged_in()) { echo '<p class="text-center">You’re already logged in! Enjoy the content.</p>'; } else { // Display login form with redirect to current page $login_args = array( 'redirect' => get_permalink(), 'label_log_in' => 'Log In to Access This Page', 'remember' => true, ); wp_login_form($login_args); // Display register link with redirect parameter $register_url = wp_registration_url() . '?redirect_to=' . urlencode(get_permalink()); echo '<p class="text-center mt-3">Don’t have an account? <a href="' . esc_url($register_url) . '">Sign up here</a></p>'; } return ob_get_clean(); }
Now just add [restricted_access_form] to any restricted page, and the form will show up for non-logged-in users—with the redirect set to that exact page.
If you’re using Elementor, Beaver Builder, or another page builder, you can use their built-in form widgets to handle this:
- Elementor: Add a Login or Registration widget. In the widget settings, go to the Redirect option, select Dynamic Tags, then choose Current Page URL.
- Beaver Builder: Add a Login Form module. Under Advanced settings, set the Redirect URL to
{current_page}(Beaver Builder’s dynamic tag for the current page).
Quick Tips
- Always test the flow in incognito mode to make sure the redirect works as expected.
- Clear your site cache after adding code or changing plugin settings—cached pages might not pick up the new redirect rules.
- For security, make sure you use
esc_url()andurlencode()when handling redirect URLs (the code above already does this).
内容的提问来源于stack exchange,提问作者DevBeats

