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 to previous page 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.

This Doesn’t Work!

For the code above to work, your login form must be embedded in one of you theme’s templates, or in a post or page.

If you’re providing users a “Login” button that takes them to the default login page (wp-login.php), this won’t work for you, users will be sent to the WordPress dashboard instead of the page they were at before hitting wp-login.php.

If the code above doesn’t work (ie: users are logging in at wp-login.php), the code below will allow you to define a page to redirect users to after login:

The example above will redirect users (including administrators) to /members/, and uses the home_url() WordPress function. So, if I were using this here, users would be redirected to https://longren.io/about/.

If you want to redirect users based on their role, like redirecting Administrators to the dashboard like normal, check out the example provided in the login_redirect filter documentation.


Well, now what?

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

Longren.io is proudly hosted by DigitalOcean


29 thoughts on “WordPress Tip: Redirect to Previous Page After Login

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

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

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

  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

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

    1. I’ve heard people reporting this elsewhere, too. There’s even a topic on the WordPress.org forums about it:

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

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

  4. I’ve been searching for this code for weeks, and thankfully this one works for me. Thanks a lot.

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

  5. It works perfectly for me too, without any modification.
    However there is one restriction: it redirects me to my home page when I log in from link in the Meta widget I placed in my site footer.
    Do you know if solving that is possible?

  6. hi i am new with wordpress, this tip is awesome.. this only apply when I login a success credentials but if failed/invalid it won’t work. do you have a demo with this?

  7. can we redirect back from custom login page>> see example of this site https://goo.gl/bNxlXi try clicking download solutions it link to login page and drop the url as some /?next/ which hits back when user logged in….can we implement this to wordpress


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.