Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress redirect to referring page after logging in

Note: I already created the same question in Wordpress StackExchange, but I didn't receive any responses! Sorry about that!

I am not using any custom login plugins or any bespoke code. A few of my pages have got this bit of code in them at the very beginning.

<?php
    if(!is_user_logged_in())
        wp_redirect('/login/');
?>

So, this doesn't allow the users to view the page when not logged in. I have these pages bearing this bit of code:

/wp-content/my-theme/my-account/
/wp-content/my-theme/my-account/world.php
/wp-content/my-theme/my-account/subscription.php
/wp-content/my-theme/my-dashboard.php
/wp-content/my-theme/my-files.php

Now, when a user goes to any of the above pages, without logging in, it redirects to the login page, and when the user logs in, it lands them to the my-account/ page.

I want to change the current scenario to make the user redirect to the referring page, where he came from. I tried the following things, which never worked.

Using a HTTP_REFERRER

In the login/ form, I placed this bit of code:

<input type="hidden" name="redirect" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />

Hacking functions.php

In the functions.php, I placed this bit of code:

if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
        add_filter('login_redirect', 'my_login_redirect', 10, 3);
        function my_login_redirect() {
                $location = $_SERVER['HTTP_REFERER'];
                wp_safe_redirect($location);
                exit();
        }
}

References:

  • Redirect wordpress back to referring page after login
  • WordPress Tip: Redirect to Previous Page After Login

I have also tried these and failed:

  • Redirecting users to referrer page after logging in using custom login form
  • Redirect user to original url after login?

Nothing was working out. I am happy to provide further details if needed. Thanks in advance. :)


My work so far...

I have modified the code this way:

<?php
    if(!is_user_logged_in())
        wp_redirect('/login/?redirect_to=' . $_SERVER["REQUEST_URI"]);
?>

This renders the login page this way:

/login.php?redirect_to=/my-account/subscription.php

This would be enough for me to authenticate and redirect. But I need to find the bit where the real redirection happens and I want to redirect it using the redirect_to parameter!

like image 280
Praveen Kumar Purushothaman Avatar asked Nov 27 '14 13:11

Praveen Kumar Purushothaman


People also ask

How to set up login redirects in WordPress?

Repeat the process if you need to set up redirects for more individual users. You can also set up a login redirect based on user role in WordPress. You simply need to select a user role from the drop down list and then enter the redirect URL. For example, you can redirect editors to the admin-area and subscribers to a custom page.

How to redirect a user back to the original referring page?

To redirect a user back to the original referring page, on the page itself, set the option to redirect after login to a custom URL and then leave the URL blank. Doing so will revert it back to that page after login. If the user has access, they will go to that page; if not, they will go to their profile.

Why does the WordPress login page keep refreshing and redirecting?

If you the WordPress login page keeps refreshing and redirecting you back to the login form, then this is a sign of a conflicting setting. For troubleshooting this problem, see our guide on how to fix WordPress login page refreshing and redirecting issue.

How to redirect a user on login and logout?

The first option on the settings page allows you to redirect only specific users. You can select a username from the drop-down menu. After that you can enter the URLs to redirect a user on login and logout.


1 Answers

The login process is handled via AJAX. The function posting the values isn't considering redirect_to . The following code in the JS file always redirects to /my-account. So now when the AJAX function returns, you can get the value of redirect_to hidden field and then redirect the user there.

window.location = "/my-account/";
like image 112
Anand Shah Avatar answered Oct 28 '22 09:10

Anand Shah