Redirect WordPress Back to Referring Page After Login

Snippets by IsItWP

Do you want to redirect users back to the page they were viewing after logging in? While there’s probably a plugin for this, we have created a quick code snippet that you can use to redirect WordPress back to referring page after login.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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'];

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes and how to create a contact form in WordPress.

Comments   Leave a Reply

  1. Thank you for your article.
    Could you give the complete code snippet for the same action with WooCommerce because I tried adding the different pieces of code but I’m afraid I’ve done some errors. The result has failed.
    Thank you very much in advance.

    And is there the same code for the logout process?

  2. I want any user that login from a page say https://example.com/asset/* is redirected back to that page after login. What do i have to do to make this work

    1. Are you only wanting to only redirect on https://example.com/asset/* ?

      The above snippet should redirect the page URLs in general.

  3. Hello, does the code work well with Ultimate Member plugin?

  4. Hello,
    I can’t make it work.
    I use this:

    function custom_login(){
    global $pagenow;
    $substitute_login_page = str_replace(‘wp-login.php’,’my-account’, $pagenow);
    if( ‘wp-login.php’ == $pagenow && $_GET[‘action’]!=”logout” && $_GET[‘action’]!=”lostpassword”) {

    to use the Woocommerce login page instead wp-login.php (my website is online shop). And it works.

    I try to return user in blog post/single product page viewed before login using the filter listed here ( add_filter(‘woocommerce_login_redirect’, ‘wc_login_redirect’); ), but it’s not working.
    I guess the filter and my action can’t work together. I think Woocommerce login page strips redirect part from url.

    How can I achieve redirect to previous page after login in my-account page?

    Thank you in advance!

    1. Sadly there could be a number of causes. You may want to try a different priority for the filter.

  5. WOW!! that one worked for me perfectly. thankyou

    1. Glad it helped, Vinoth

    2. How do I have to modify this code to redirect users after a sign up?

      1. Hey Ole, I hope this snippet can help you out: https://www.isitwp.com/redirect-a-successful-registration-page/

        If you have further questions, please feel free to reach out.

  6. Hello sir,

    I am using my own code for registration and login. How to use there the above mention code.Plz help

  7. i want to this other way
    i want to show the product prior to download or purchase want to make login in easy digital download

    1. Not sure, I totally understand what you are looking for. Can you go into a little more detail.

      1. I want to show login to download button first and if user register and login then site should show prices and if clicked it should go to download as showed in image

  8. This does not work for me using WP 4.1.1 with the FULLBY theme and Simple WordPress Membership plugin

  9. Karena Laubinger Johnstone February 1, 2015 at 1:25 pm

    Hi, this didn’t change anything for me. Any suggestions? I’ve been looking for this solution for a while.

    1. when you login to test this script what page are you on? are you on the wp-login.php page, or another page.

      1. Karena Laubinger Johnstone February 1, 2015 at 2:00 pm

        Yes, on the wp-login.php page.

        1. This would be the reason, the idea behind this snippet is that if you have a login form in your sidebar or another place for example. I would be reading the post then enter my details to login, I would then login and be forwarded back to the page I login from.

          1. Karena Laubinger Johnstone February 1, 2015 at 2:26 pm

            Okay, thank you. So, I am just using the standard WP Meta Login widget. When in a post before commenting you must login. In the sidebar is the Meta/Login widget which takes you to the wp-login.php page when you click ‘Log In.’ Once logged in on the wp-login.php page, it returns the visitor to the Homepage always, as opposed to the post they were viewing beforehand. Please let me know if your code will work somehow for this. Thank you!

          2. Hi Karena,
            send me a message via the contact form, will be easier. Comments can get a bit long.

          3. I too really need this. My news page restricts contents to subscribers only. When a subscriber loads up an article, the error message is displayed but they can always login using the widget form in the sidebar. My problem is that when they successfully login they are not redirected to the last article they are in.

  10. Does anyone know how to do this for a woo-commerce login?

    1. Give this a try,

      add_filter(‘woocommerce_login_redirect’, ‘wc_login_redirect’);

      function wc_login_redirect( $redirect_to ) {
      $redirect_to = ‘http://mysite.com/shop’;
      return $redirect_to;

      1. But to redirect you back to the referring page, not a static page?

        1. should be able to use the code in the above sample, but I would first check to make sure that login code works then just replace

          $redirect_to = ‘http://wpsnipp.com’;
          return $redirect_to;


          $location = $_SERVER[‘HTTP_REFERER’];

          1. What is the use of $redirect_to after replacement. Code is not working for me 🙁

          2. Hi Aayush, this should work without any issues, when a user logs into woo commerce they will be redirected in this case to wpsnipp.com. Place this within the functions.php of your wordpress theme, also make sure that you have woo commerce installed otherwise this will not work.

            add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
            function wps_login_redirect( $redirect_to ) {
            $redirect_to = ‘http://wpsnipp.com’;
            return $redirect_to;

          3. Mousa Abu Doush June 8, 2016 at 4:24 pm

            Hello Kevin,
            I tried this code and it works:

            add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
            function wps_login_redirect( $redirect_to ) {
            $redirect_to = ‘http://wpsnipp.com’;
            return $redirect_to;

            But this one didn’t work:

            add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
            function wps_login_redirect( $redirect_to ) {
            $location = $_SERVER[‘HTTP_REFERER’];

            I would appreciate if you could help me,

          4. I would guess that HTTP_REFERER is empty.

            The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature.

          5. Thank You. It is worked for me

  11. Has anyone had success w/ this code integrated w/ any S2Member websites? The way the S2Member plugin handles the post-login redirect may be overriding the code suggested.

    For good measure, in addition to creating a functionality plugin, I’ve updated the functions.php file itself within my parent theme, as well as the functions.php within the child theme which my site actually uses. I made sure that the post variable ID was being updated for the one that the WordPress login page actually uses (“user_login”), as opposed to “login_location”.

    Looking forward to hearing of any experience w/ successfully redirecting w/ sites that have the S2Member plugin installed and activated.


    1. Hi Darren,
      s2 has some built in functions for login redirect eg:

      add_filter(“ws_plugin__s2member_login_redirect”, “s2_redirect”, 10, 2);
      function s2_redirect($redirect, $vars = array()){
      // bla bla bla code……
      return $redirect;

      I have not tested this but something along this lines could work for you.

      if ( (isset($_GET[‘action’]) && $_GET[‘action’] != ‘logout’) || (isset($_POST[‘login_location’]) && !empty($_POST[‘login_location’])) ) {
      add_filter(“ws_plugin__s2member_login_redirect”, “s2_redirect”, 10, 2);
      function s2_redirect($redirect, $vars = array()){
      $redirect = $_SERVER[‘HTTP_REFERER’];
      return $redirect;

  12. This doesn’t work for me. Incompatible with 3.8.1?

  13. This doesn’t work for me. Incompatible with 3.8.1?

  14. This doesn’t work for me. Incompatible with 3.8.1?

  15. Wouldn’t it logically be more correct to simply use

    add_filter(‘login_redirect’, ‘my_login_redirect’, 10, 3);

    And then move the if statement into the function itself?

  16. Which page would I put this on? Are we talking about a page w/ a link to the login page, the login page, and the redirected page?

    1. Place this into the functions.php and will redirect them to the page they had been viewing after they login.

      1. AWESOME! Even better than I was thinking it could be…

  17. Great! Thanks

    1. Cool glad that you like the snippet.

  18. Nice!

    1. Ya I agree Justin!  –  Tyler wrote this after needing it for work I’m sure ill be using this one for a project soon.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!