简体   繁体   中英

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). 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. They click on the URL in the email and are sent their page with a login link. 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

<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

$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. Also it is more reliable to check the user's role using the in_array() function.

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)

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/

And another stackoverflow thread where this problem is thoroughly discussed: Redirect after Login on Wordpress

The best code I have found so far is below (although there is 1 big flaw in this for me):

// 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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