如果用户未登录,WP重定向至自定义登录页面

时间:2021-06-28 作者:Muaz Hesam Musa

我想在用户观看我们网站的内容之前登录,但我总是处于重定向循环中,我不知道为什么。但我搜索了所有的google和stackoverflow,但没有找到它的解决方案。请看一下我的密码

function redirect_login_page() {
    if ( ! is_user_logged_in() ) {
        wp_redirect( home_url(\'/account/\') );
        exit;
    }
}
add_action( \'template_redirect\', \'redirect_login_page\' );
我也试过这个

function redirect_login_page() {
    if ( ! is_user_logged_in() ) {
        wp_redirect( \'https://playground.com/account/\' );
        exit;
    }
}
add_action( \'template_redirect\', \'redirect_login_page\' );
上面的作品都不完美。我总是这样....redirected you too many times.

但根据wp\\U重定向,可能存在任何问题。我也用过这个wp_redirect( home_url(\'/account/\'), 302 ); / wp_redirect( home_url(\'/account/\'), 301 ); etc等

1 个回复
SO网友:bosco

您正在为访问者的所有请求建立重定向,包括/account:

请求/, 用户未登录-重定向到/account./account, 用户未登录-重定向到/account./account, 用户未登录-重定向到/account.

  • (等)
    • 要中断循环,仅当请求不是针对您的登录页面时,才执行重定向:

      function redirect_login_page() {
          if ( ! is_user_logged_in() && ! is_page( \'account\' ) ) {
              wp_redirect( home_url(\'/account/\') );
              exit;
          }
      }
      add_action( \'template_redirect\', \'redirect_login_page\' );
      

    相关推荐

    Updating modified templates

    我想调整一些。php模板文件在我的WordPress主题中,我知道正确的过程是将相关文件复制到子主题文件夹中并在那里编辑文件,这样我的修改就不会在将来的主题更新中丢失。但这是否意味着我将无法从主题更新中获益,因为我将文件放在了我的子主题文件夹中?这可能不是一件好事,因为主题更新可能添加了一些有用的特性,甚至修复了我最初需要对代码进行调整的问题!这方面的常见解决方案是什么?有人推荐了一款Diff应用程序——这是人们常用的吗?