WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Any way to redirect to previous page after login (5 posts)

  1. senorwooly
    Member
    Posted 5 years ago #

    After users login at my site, I want to redirect them to the page they were previously at.

    For example, users at 'Page A' click on the Login link. They get taken to the WordPress login page. Then, after successfully logging in, I want them redirected back to 'Page A'.

    I know about the redirect plugin, but that only takes you to specific, pre-determined pages.

    Can anyone help me on this?

    Thanks,
    Sr. Wooly

  2. t31os
    Member
    Posted 5 years ago #

    Not ideal because it requires editting a core WP file... but since you asked... it can be done like so...

    wp-login.php

    Line 424 - 426...

    } else {
    		$redirect_to = admin_url();
    	}

    Change to...

    } else {
    		$redirect_to = $_SERVER[HTTP_REFERER];
    	}

    Not sure how reliable that is, but it does archieve the desired result...

  3. senorwooly
    Member
    Posted 5 years ago #

    Thanks so much. That works. Just have to make a note about the change so I can do it again after updating WP.

    Much appreciated.

  4. NerdSpawn
    Member
    Posted 4 years ago #

    Was wondering if this would be an option for my site...

    We've got lots of private content which users can only access after they log in. How it currently works:

    1. Members get sent a URL via email to a private document/page located on the site.
    2. They click the URL and if they're not logged in, they get the 404 page.
    3. On the 404 page, they're able to click through to the login page.
    4. After logging in, they get redirected to the home page.

    Is it possible to redirect to the URL they were originally trying to access before getting the 404 page (and then logging in)? Hope that makes sense... any help and/or advice is greatly appreciated. :)

  5. Casemon
    Member
    Posted 4 years ago #

    Thanks for this solution, not ideal given, as you say, the need to edit core files (ahhh!), but it works a treat!

    Likely (hopefully) this will be addressed in a future WP release.

    @NerdSpawn Sounds like maybe you should add a PHP variable to your 404 template that stores the referrer, then use that variable instead of referrer.

    Hope this helps!

Topic Closed

This topic has been closed to new replies.

About this Topic