Home / admin / Redirect wordpress back to referring page after login
  • How to Redirect wordpress back to referring page after login

    redirect-back-to-referring-page-after-login

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

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    login_redirect, add_filter, wp, wp_safe_redirect,
  • Nice!

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

  • Marlon Amancio

    Great! Thanks

  • Anonymous

    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?

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

      • Anonymous

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

  • Bryan

    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?

  • Daniel Hansen

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

  • Daniel Hansen

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

  • Daniel Hansen

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

  • 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

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

  • Matt Foreman

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

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

      • Matt Foreman

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

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

  • Karena Laubinger Johnstone

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

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

      • Karena Laubinger Johnstone

        Yes, on the wp-login.php page.

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

          • Karena Laubinger Johnstone

            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!

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

          • Karen Moreno

            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.

  • Nobby2

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