将登录用户从特定页面重定向到 Woocommerce 我的帐户页面

Redirect logged in users from a specific page to Woocommerce my account page

在 Woocommerce 中,我试图找到一种解决方案来检查用户是否已在自定义页面上登录,如果是,则将用户重定向到 "My Account" 页面。

如有任何帮助,我们将不胜感激。

您应该使用 $_SESSION。 这有助于您验证用户是否在页面中登录。

if(isset($_SESSION['UserID'])){
    header('Location: [url]');
}

使用 Wordpress:

    $current_user = wp_get_current_user();
    if ( 0 != $current_user->ID ) {
        $template = get_page_template_slug($post->ID);
        if($template == "your_custom_template_name"){
             wp_redirect( wp_login_url() )    
        }   
    }

尝试以下操作,将 'some-page' 替换为您的真实页面 ID、别名或名称。该代码将针对登录用户定义的特定页面重定向到我的帐户页面:

add_action('template_redirect', 'specific_logged_in_redirect');
function specific_logged_in_redirect() {
    if ( is_page('some-page') && is_user_logged_in() ) {
        wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
        exit();
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。


对于 2 个页面,您将使用:is_page( array( 'some-page', 'some-other' ) )