Log In!

in Internet

WordPress Tip: Redirect to Previous Page After Login

I work for a group of newspapers here in Iowa. We recently started moving these sites to WordPress. Site visitors must be logged in to view stories. They can see all the stories on the front page, but when they click through to a single story, they see a login form in place of the post/story content.
[ad]
I needed to redirect users back to the page they were viewing after logging in. So, if a user was viewing a story called “Look at me now”, they’d need to be redirected back to that story after logging in.

To achieve this redirect after login, add the following code to the functions.php file for your theme:

I looked at a number of plugins to do this, but none seemed to offer this functionality.

A bit of searching on Google yielded this post at Taproot Creative. I modified the code on that post to set the redirect location to the referring page, and that was it!

Now users are redirected back to the story/post they originated from.

Where to now?

Work with Me

Think you'd need help getting somethink like this setup on your own? Or maybe you've got an idea for a product but don't know how to turn it into 'something'. I can help on both fronts! Just get in touch and we can chat about your needs.

Leave some Feedback

Got a question or some updated information releavant to this post? Please, leave a comment! The comments are a great way to get help, I read them all and almost always reply to every comment.

Enter your email address to subscribe and receive notifications of new posts by email.

  • http://www.priteshgupta.com/ Pritesh Gupta
    • http://tyler.longren.org/ Tyler

      I need to have as much as I can integrated into certain themes, instead of relying on various plugins.

      Looks like a good plugin you made though. I wrote a similar one a while ago, but never released it.

      • http://www.priteshgupta.com/ Pritesh Gupta

        Ohh, that’s something else… Glad you like it though! :-)
        I just saw this post so thought it will be cool to drop by…

        A little suggestion: if you like the functionalities of this plugin, you can copy-paste the php code into theme’s functions.php. Of course I won’t mind the credits and stuff with you. :-)

  • Pingback: T. Longren » HTML5Press Post Titles Change

  • Keith

    Brilliant piece of code – dropped it into fuctions.php and it worked first time. No need for a plugin. Smashing stuff, thank you very much!

    • http://tyler.longren.org/ Tyler

      Hi Keith,

      After writing this, I ended up putting this snippet into a functionality plugin for the various newspaper sites. That way when we decide to switch themes, we can still keep that functionality with a new theme, via the functionality plugin.

      May be something you’d want to look into as well. There’s a good article on WPCandy about creating a functionality plugin:
      http://wpcandy.com/teaches/how-to-create-a-functionality-plugin

  • http://www.vibrancephoto.co.uk Matt

    Thanks – Took a while of googling but yours was the only solution that actually worked without being too heavy handed with plugins and the like!

  • Bas

    Hi,

    Thanks for this! Is it also possible to use it on pages? Now it only works on posts.

    Thanks!
    Gr, Bas

    • http://www.longren.org/ Tyler

      It’ll work anywhere. It checks where the user was sent from, that can be a page, post, archive page, tag page, category page, or whatever really.

  • thetennisbulletin

    does not work for me.

    • http://www.longren.org/ Tyler Longren

      I’ve heard people reporting this elsewhere, too. There’s even a topic on the WordPress.org forums about it:
      http://wordpress.org/support/topic/login-redirect-to-previous-page?replies=6#post-4577931

      I haven’t had time to really figure out why it doesn’t work any longer. I probably won’t have time for at least another year (pretty booked up solid, it’s good though).

      If anyone wants to contribute and look into why it’s not working, and how to get it working again, I’d be glad to give you credit in the post. :)

  • thomas chuntic

    You can use this directly echo ‘Login‘; Just remove the spaces, I did that just to display directly the code.

  • thetennisbulletin

    this code does not work

    • http://www.longren.org/ Tyler Longren

      Yah nothing has changed since 4 months ago, unfortunately.

  • Jen

    Thanks. Worked for me!

    • http://www.longren.org/ Tyler Longren

      That’s awesome! Did you have to make any modifications?

      I’ve randomly gotten it to work, yet it won’t on other installs. Need to really track down what the issue is that causes it to sometimes not work.