php 将 Wordpress 的登录/注册页面重定向到自定义登录/注册页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1976781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:31:30  来源:igfitidea点击:

Redirecting Wordpress's Login/Register page to a custom login/registration page

phpwordpressredirectloginregistration

提问by Bruno De Barros

I have a website, with a user system. I want to integrate wordpress's user system into that website's, but I still want to use the website's register/login pages. I don't want anybody to be able to login or register using Wordpress's login or registration forms. Instead, when they're try to access the login/registration pages in Wordpress, I want those pages to redirect them to my own login/registration pages.

我有一个网站,有一个用户系统。我想将 wordpress 的用户系统集成到该网站的用户系统中,但我仍然想使用该网站的注册/登录页面。我不希望任何人能够使用 Wordpress 的登录或注册表单登录或注册。相反,当他们尝试访问 Wordpress 中的登录/注册页面时,我希望这些页面将它们重定向到我自己的登录/注册页面。

Is there any way of doing this? I've tried Google, but all I could find was redirection AFTER the user logs in or registers, which is not what I want.

有没有办法做到这一点?我试过谷歌,但我能找到的只是用户登录或注册后的重定向,这不是我想要的。

Thank you in advance.

先感谢您。

回答by nickohrn

For this you need to redirect login/registration page to your custom pages. So, Write this code in your functions.phpfile Under your activated theme folder. Pass your custom page path as a Argument.

为此,您需要将登录/注册页面重定向到您的自定义页面。因此,在您的functions.php文件中编写此代码在您激活的主题文件夹下。将您的自定义页面路径作为参数传递。

 add_action('init','possibly_redirect');

function possibly_redirect(){
 global $pagenow;
 if( 'wp-login.php' == $pagenow ) {
  wp_redirect('http://google.com/');
  exit();
 }
}

回答by Anatoly

To restrict direct access only for 'wp-login.php', without POST or GET request (useful for custom ajax login forms), I use the advanced function:

为了限制仅对 'wp-login.php' 的直接访问,没有 POST 或 GET 请求(对自定义 ajax 登录表单很有用),我使用了高级功能:

function possibly_redirect(){
  global $pagenow;
  if( 'wp-login.php' == $pagenow ) {
    if ( isset( $_POST['wp-submit'] ) ||   // in case of LOGIN
      ( isset($_GET['action']) && $_GET['action']=='logout') ||   // in case of LOGOUT
      ( isset($_GET['checkemail']) && $_GET['checkemail']=='confirm') ||   // in case of LOST PASSWORD
      ( isset($_GET['checkemail']) && $_GET['checkemail']=='registered') ) return;    // in case of REGISTER
    else wp_redirect( home_url() ); // or wp_redirect(home_url('/login'));
    exit();
  }
}
add_action('init','possibly_redirect');

回答by brasofilo

The correct action hook is login_initthat only fires on wp-login.php.

正确的动作钩子是login_init只触发wp-login.php.

Here, no ?action=action-nameis being requested, so it's the main login page:

在这里,没有?action=action-name被请求,所以它是主登录页面:

add_action('login_init', function(){
    if( !isset( $_GET['action'] ) ) {
        wp_redirect( 'http://example.com' );
    }
});

For all requests, we can use a specific hook login_form_ACTION-NAME, ie, postpass, logout, lostpassword, retrievepassword, resetpass, registerand login. Example:

对于所有的要求,我们可以使用特定的挂钩login_form_ACTION-NAME,即postpasslogoutlostpasswordretrievepasswordresetpassregisterlogin。例子:

add_action('login_form_register', function(){
    wp_redirect( site_url('custom-registration/') );
});

回答by cee.e

If you're making use of a custom login page, but still using wp_login_form(), be aware that the form will POST to wp-login.php, so you will want to check if $_POST is empty before redirecting.

如果您正在使用自定义登录页面,但仍在使用 wp_login_form(),请注意表单将 POST 到 wp-login.php,因此您需要在重定向之前检查 $_POST 是否为空。

function prefix_wp_login_redirect() {
  global $pagenow;  
  if( $pagenow == 'wp-login.php' && empty($_POST)) {
    auth_redirect();
    exit();
  }
}

回答by ceejayoz

You might be able to latch onto the login_headhook and issue a redirect there.

您可能能够锁定login_head钩子并在那里发出重定向。