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.

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.

If you use a functionality plugin, which you really should, it’d make an ideal home for this snippet of code. So if you can, drop it in a functionality plugin instead of the functions.php file for your theme.

Where to now?

Work with Me

I'm available for hire and always taking new clients, big and small. Got a project or an idea you'd like to discuss? Startup plan but no developer to make it happen? Just get in touch, I'd love to see if I can help you out!

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 reply to nearly every comment. Let's talk. :)

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

Leave a Comment



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

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

  3. Hi,

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

    Gr, Bas

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

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

    • That’s good to hear, Ritchie. I do believe the code didn’t work for some time, for whatever reason. Did you have to make any significant modifications? Or any modifications at all?