重定向用户,如果他们没有登录,只有当他们在某个页面上


WORDPRESS: Redirect a user if they are not logged in, ONLY if they are on a certain page

我尝试了多种方法/插件和编辑我的functions.php文件,但是,我无法找出或找到一种适合我的方法。当用户访问有问题的页面时,我能够重定向,但是我无法将其限制为仅当他们未登录时。

我的用户将访问段塞:

example.com/feature-page/

如果他们没有登录,我想把他们重定向到:

example.com/dashboard/

如果你有解决办法,我很乐意听听。谢谢你!

将此添加到您的header(header.php)中,它应该可以正常工作。

if(!is_user_logged_in()&&is_page('feature-page')) {
  header("Location: http://www.example.com/dashboard/"); 
  //remember to replace "example.com" with your domain
}

wordpress中有一个内置的函数:

https://codex.wordpress.org/Function_Reference/is_user_logged_in

if(!is_user_logged_in()) {
  // user is not logged in
  // execute your code here
}

使用wp_redirect()函数重定向并检查用户是否登录,is_user_logged_in()函数是否登录用户重定向。例如

if(is_user_logged_in()) {
 wp_redirect( get_permalink( $your_page_ID )); exit;
#example.com/dashboard/
}
else{
wp_redirect(home_url()); exit;
#example.com/feature-page/
}
相关文章: