After the member logs in and views the content.. whatever page they last visited at time of logout is the the page they are returned to after logging back in.

I found the code to accomplish this and have it installed on my site


Whenever a user logs in they are still taken to the page I have set in the Membress plugin instead of the last page they visited before log out.

Here is the link to the code

and here is the actual code:

// # store the last known URL to session
function remember_last_page() {
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$url = filter_var($referrer, FILTER_SANITIZE_URL);

if(!empty($url)) {

// # Check if session exists, if not start one - PHP 5.4+ required for session_status()
if (session_status() == PHP_SESSION_NONE) {

// # prevent looping back to the login page!
$tld = (stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://'.$_SERVER['HTTP_HOST'].'/' : 'http://'.$_SERVER['HTTP_HOST'].'/');
if (strpos($url, 'login') === false || $url == $tld) {
$_SESSION['last_page_visited'] = $url;
add_action('init', 'remember_last_page');

// # redirect user to last requested page if session contains 'last_page_visited'
function login_redirect_to_last() {

if(!empty($_SESSION['last_page_visited'])) {
$url = filter_var($_SESSION['last_page_visited'], FILTER_SANITIZE_URL);
return $url;
add_filter( 'login_redirect', 'login_redirect_to_last');


Am I right to assume the redirects I have set for members logging is over riding what the above code is doing.

If so, I guess the idea is to somehow add to this code that if the current member has 1 login attempt ignore the redirect in membpress on next login and redirect to the last page visited like the code says to do?

Is this possible? Any advice or pointers on this topic?

Thanks in advanced,


  • Adam Czajczyk

    Hello EJ,

    I hope you're well today and thank you for your question!

    The code looks fine, I think, however it seems that your membership plugin is "overriding" it. I think I'd start with following:

    1. disable that redirect within membership plugin if possible or disable the plugin entirely in order to confirm that the code works indeed; if it does...

    2. ... play a bit with this line

    add_filter( 'login_redirect', 'login_redirect_to_last');

    by changing it to

    add_filter( 'login_redirect', 'login_redirect_to_last', 9);

    The "9" in this case is a "priority" argument and defines how soon/late that filter function should be executed. The goal here is to execute it before the membership plugin's function gets triggered but late enough for other processes to complete.

    If value of "9" doesn't help, try lowering it even more or - if going all the way down to "0" doesn't work - go opposite way increasing it. I think that should solve the issue.

    Best regards,

  • Adam Czajczyk

    Hello ME Admin!

    Could you tell me which membership plugin are you using and if it's freely available on the web so I could download, test and review it?

    If it's not available on the web, would you mind if I accessed your server directly in order to review that plugins code? For this I would need your site and FTP login credentials so if you agree (and the plugin is not free to download) please send in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include hosting control panel access details (login address, username & password) - cPanel's usually the control panel used for this, but your provider may use something else; I'll need this for accessing your site's database, preferably via phpMyAdmin
    - Include any relevant URLs for your site

    Please use our contact form here

    Please be sure to make a full backup of your entire site first!

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.