简体   繁体   English

登录后如何将WordPress用户重定向到上一页

[英]How to redirect WordPress user to previous page after login

I have a WordPress site where some/most of the pages can be viewed by anyone (not logged in). 我有一个WordPress网站,任何人(未登录)都可以查看一些/大部分页面。 However a user can request to have their own private page, so I set up a page that can only be accessed by that specific person and then email the URL to them. 但是,用户可以请求拥有自己的私有页面,因此我设置了只能由该特定人员访问的页面,然后将URL通过电子邮件发送给他们。 They click on the URL in the email and are sent their page with a login link. 他们单击电子邮件中的URL,并向其页面发送登录链接。 Once login is successful I want the user to arrive back at their private page, but currently they just end up at their profile page. 登录成功后,我希望用户返回其私人页面,但目前他们仅停留在其个人资料页面。

How can I redirect the user to their private page after login? 登录后如何将用户重定向到其私人页面?

I have tried so many different bits of code, but none have worked for this situation. 我尝试了很多不同的代码,但是没有一种适用于这种情况。

My current code is below. 我当前的代码如下。 But this just sends the user back to the login page (even though login was successful). 但这只是将用户送回到登录页面(即使登录成功)。

// Function to redirect after login
add_filter('login_redirect', 'redirect_previous_page', 10, 1);

function redirect_previous_page( $redirect_to ){
    global $user;

    $request = $_SERVER["HTTP_REFERER"];

    if ( in_array( $user->roles[0], array( 'administrator') ) ) {

        return admin_url();

    } elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

        return $request;
    } 

    return $redirect_to;
}

If you are using a login link on the homepage to go to the login page, then use the following code, 如果您使用首页上的登录链接转到登录页面,请使用以下代码,

echo wp_login_url( $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );

If you are using a custom form on the frontpage, then inside the , make sure you fill in a hidden field with the url to redirect 如果您在首页上使用自定义表单,请在内确保使用url填写隐藏字段以进行重定向

<input type="hidden" name="redirect_to" value="<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>" />

And if you are using wp_login_form() to generate the form, then follow the below code, for more information checkout this page, wp_login_form 如果您使用wp_login_form()生成表单,请遵循以下代码,有关更多信息,请查看此页面wp_login_form

$args = array(
        'echo' => true,
        'redirect' => site_url( $_SERVER['REQUEST_URI'] ), 
        'form_id' => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in' => __( 'Log In' ),
        'id_username' => 'user_login',
        'id_password' => 'user_pass',
        'id_remember' => 'rememberme',
        'id_submit' => 'wp-submit',
        'remember' => true,
        'value_username' => NULL,
        'value_remember' => false );

wp_login_form( $args );

As you know the page you are going to redirect the user to you can do that explicitly rather than getting the HTTP_REFERER. 如您所知,您将要重定向用户到的页面可以显式执行,而不必获取HTTP_REFERER。 Also it is more reliable to check the user's role using the in_array() function. 同样,使用in_array()函数检查用户角色更为可靠。

Try this: (you will need to add your own logic to get the user's private page, the example assumes the url for each user's page is yoursite.com/user/john) 尝试以下操作:(您将需要添加自己的逻辑来获取用户的私有页面,该示例假定每个用户页面的url为yoursite.com/user/john)

function redirect_previous_page( $redirect_to, $request, $user  ) {
    if ( is_wp_error( $user ) ) { return $redirect_to; }

    if ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) {
        return admin_url();
    } else if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
        return site_url() . '/users/' . $user->user_login;  // Add your logic here      
    } else {
        return $redirect_to;        
    }
}
add_filter( 'login_redirect', 'redirect_previous_page', 10, 3 );

Here is a tutorial that covers it in more depth. 这是一个更深入地介绍它的教程。 https://tommcfarlin.com/redirect-non-admin/ https://tommcfarlin.com/redirect-non-admin/

And another stackoverflow thread where this problem is thoroughly discussed: Redirect after Login on Wordpress 另一个彻底讨论了此问题的stackoverflow线程: 在Wordpress上登录后重定向

The best code I have found so far is below (although there is 1 big flaw in this for me): 到目前为止,我发现的最好的代码如下(尽管对我来说这有1个大缺陷):

// Function to redirect after login (best so far)
function redirect_after_login(){
  global $wp;
  $protocol='http';
  if (isset($_SERVER['HTTPS']))
    if (strtoupper($_SERVER['HTTPS'])=='ON')
      $protocol='https';
  if (!is_user_logged_in() && !is_home() && ($wp->query_vars['pagename'] != 'downloads') ){
    $redirect = home_url() . "/wp/wp-login.php?redirect_to= $protocol://" . 
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
    wp_redirect( $redirect );
    exit;
  }
}
add_action( 'wp', 'redirect_after_login', 3 );

This code works great when I click on a link from an email - in this case I'm directed to the login screen and then on to the page URL I originally clicked in the email. 当我单击电子邮件中的链接时,此代码非常有用-在这种情况下,我将定向至登录屏幕,然后定向至最初在电子邮件中单击的页面URL。 The big problem is that ANY non-home page also redirects me to a login screen. 最大的问题是,任何非主页也会将我重定向到登录屏幕。 This means my casual reader can't get anywhere on my website except home. 这意味着我的随便的读者只能在家里浏览我的网站。

Surely this shouldn't be so hard. 当然,这应该没那么难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM