如何根据用户角色设置多条件页面跳转规则
Alright, let's tackle these role-based redirect rules step by step—this is super common for WooCommerce setups, so I’ve got you covered. Below are tailored solutions for each rule, using custom PHP code (you can add these to your theme’s functions.php file or a dedicated custom plugin to avoid losing changes on theme updates):
Rule 1: Redirect unlogged users from Shop to Register
When a guest user tries to access your Shop page, we’ll send them directly to the Register page to encourage sign-up:
add_action('template_redirect', 'redirect_guest_from_shop_to_register'); function redirect_guest_from_shop_to_register() { // Check if user is not logged in and on the Shop page if (!is_user_logged_in() && is_shop()) { // Redirect to Register sub-page of My Account (default WooCommerce setup) wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')) . 'register/'); exit; } }
Note: If you use a standalone Register page, replace the redirect URL with get_permalink('your-register-page-id') instead.
Rule 2: Redirect logged-in users (Subscriber/Customer) from Register to My Account
If someone who’s already logged in (either role) tries to visit the Register page, we’ll bounce them to their My Account dashboard:
add_action('template_redirect', 'redirect_logged_in_from_register_to_myaccount'); function redirect_logged_in_from_register_to_myaccount() { // Check if user is logged in and on the Register page if (is_user_logged_in() && is_page('register')) { wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id'))); exit; } }
Pro tip: If your Register page uses a different slug (like "sign-up"), replace 'register' with your actual page slug.
Rule 3: Redirect non-Customer Subscribers from restricted pages
Since you mentioned "related pages" but didn’t specify, I’ll assume you mean customer-only areas like Checkout, Order History, or exclusive product pages. Here’s a flexible snippet to lock those down:
add_action('template_redirect', 'redirect_subscribers_from_restricted_pages'); function redirect_subscribers_from_restricted_pages() { $current_user = wp_get_current_user(); // Check if user is a Subscriber (not a Customer) and on restricted pages if (is_user_logged_in() && in_array('subscriber', $current_user->roles) && !in_array('customer', $current_user->roles)) { // Define restricted page slugs/IDs here $restricted_pages = array('checkout', 'order-history', 'premium-products'); if (is_page($restricted_pages) || is_checkout()) { // Redirect to My Account (swap with an upgrade page URL if needed) wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id'))); exit; } } }
Adjust the $restricted_pages array to match the pages you want to restrict. You can also change the redirect target to an "Upgrade to Customer" page if you want to guide subscribers to convert.
Quick Tips:
- Always test these snippets on a staging site first to avoid breaking your live site.
- If you’re not comfortable with code, you can use a role management plugin to adjust capabilities, but custom code gives you precise control over redirect behavior.
内容的提问来源于stack exchange,提问作者Kenny Amaro

