用户登录时如何将主页重定向到另一个页面[重复]

问题描述 投票:0回答:1

我有一个 WordPress,用户可以注册并成为会员,例如,如何将我的主页 https://sample.com 重定向到设置页面 https://sample.com/setting。因此,当用户登录时,他们无法访问主页https://sample.com

php wordpress authentication redirect
1个回答
1
投票

有很多变化。可以使用

wp_safe_redirect
wp_redirect

如果主页是您的首页,类似这样的内容就可以完成工作:

function add_login_check()
{
    if (is_user_logged_in() && is_front_page()) {
            wp_safe_redirect('https://sample.com/dash'); # Using wp_safe_redirect
            exit; 
        }
    }
}

如果您知道页面的 ID(要检查如何查看页面的 ID,请参阅下面的注释),可以使用类似的内容

function add_login_check()
{
    if ( is_user_logged_in() && is_page(765)) {
        wp_redirect('https://sample.com/dash'); # Using wp_redirect
        exit;
    }
}

或者

function add_login_check()
{
    if (is_user_logged_in())) {
        if (is_page(765)){ # If the user is in the page with the ID 765
            wp_safe_redirect( get_permalink('234')); # To get the URL of a specific page from the ID, and using wp_redirect
            exit; 
        }
    }
}

注意: 要检查页面的 ID,请访问 WP 管理仪表板中的页面部分并滚动到页面顶部。见下图(来源)

或者,如果访问页面或帖子,还应该能够在 URL 上看到页面 ID。见下图(来源)

© www.soinside.com 2019 - 2024. All rights reserved.