X

Redirect wordpress back to referring page after login

LayerSlider Review - add layers

Adding this snippet to the functions.php of your wordpress theme will redirect visitors to the page they were viewing after logging in.


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();
	}
}

Comments  Leave a Reply

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

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

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

  4. 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;

          with

          $location = $_SERVER[‘HTTP_REFERER’];
          wp_safe_redirect($location);
          exit();

          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’];
            wp_safe_redirect($location);
            exit();
            }

            I would appreciate if you could help me,
            Regards

          4. I would guess that HTTP_REFERER is empty.

            http://php.net/manual/en/reserved.variables.server.php
            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

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

    Cheers,
    Darren

    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;
      }
      }

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

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

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

  9. 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?

  10. 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…

  11. Great! Thanks

    1. Cool glad that you like the snippet.

  12. 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!