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.

About Tyler

I'm nearly thirty, and have a beautiful daughter and fiance (wife as of August 24, 2013). I work my day job at a local web development shop, and it's awesome there. Seriously, I've never loved my job like this. Anyway, I like PHP, jQuery, WordPress, Github, HTML5 & CSS3, and other neat things. I like the open source community too. You can find me on twitter or Google+, and Github.

9 Responses to WordPress Tip: Redirect to Previous Page After Login

    • Tyler says:

      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.

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

  1. [...] got some pretty long post titles around here, and I know some other people using HTML5Press do as well. [...]

  2. Keith says:

    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!

  3. Matt says:

    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!

  4. Bas says:

    Hi,

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

    Thanks!
    Gr, Bas

    • Tyler says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">