Users to the site can register and log in and access a special page members.php, which is the home page of their account.

But if they leave the site and come back (provided they are still logged in) they need to be landing on members.php and not my regular home page.

Now everyone, logged in as well as logged out, folks land on the index.php page of my site.

How to make the above possible?

  • HTTP is a per request model, this functionality fails if a user has multiple tabs or windows open with your site, they may leave and come back as you intended, but they may have had another tab open and actively used the entire. Instead perhaps you'd prefer to redirect members on the homepage to your members page? ( note that members.php is just a template, redirecting to a template is a misleading statement, whereas redirecting to a members page that uses the members.php template is accurate and prevents subtle but confusing mistakes )
    – Tom J Nowell
    Apr 4 '17 at 20:19

Add this piece of code to your theme's functions.php file:

    if (is_user_logged_in() && !is_page('YOUR PAGE SLUG') && empty($_SERVER['HTTP_REFERER'])) { 
        wp_safe_redirect( site_url('/members.php')); 

Assuming that your members.php is located at http://example.com/members.php. This will redirect any logged in users to that page.

  • 1
    Appreciate the response Jack. But now when I first try and login as a user, this is the error it gives: ERR_TOO_MANY_REDIRECTS. When users log in they are also sent to that same page automatically (members.php). Apr 1 '17 at 1:11
  • I thought you wanted the logged in users to be redirected? You can use it in your index.php file, if you want to redirect them from homepage.
    – Jack Johansson
    Apr 1 '17 at 1:13
  • Yes, logged in but returning..:) That was the problem I was having; when users who were already logged in but were returning to the site after a few days. Like you said, I think putting this in the index.php is probably the way to go. I have no clue. Any way this can be made into a function so that I can add to the functions.php page...?? Apr 1 '17 at 1:18
  • You can try adding if(is_home() && is_user_logged_in()) to make it work only on homepage.
    – Jack Johansson
    Apr 1 '17 at 1:20
  • 1
    I think the correct condition should be: is_user_logged_in() && is_home() && empty($_SERVER['HTTP_REFERER']) && ! is_page('members.php'). So, that means: if he is logged in + if he comes to home page + if he doesn't have a referer + if he is NOT already in that member page => then redirect him to the member page. Of course I'm assuming member.php is a page controlled by WordPress.
    – Scott
    Apr 4 '17 at 20:35

There is no way to determine "they leave the site". Instead you will need to check user inactive time. For that you will need to track User's login session. Check this plugin, if it helps https://wordpress.org/plugins/idle-user-logout/

  • Appreciate the response. I get your point. But I really need to know when they "land on the home page" so that it can then somehow figure out whether there is a "logged-in value" in the cookie or not. They may "leave the site" as a result of closing browser or navigating away to a different site or shutting they computer down. But as long as I perform this "whether user is logged in or not" check, when they come back to the site, we can then take them to whatever page we want. But I have no idea how to do this. Apr 1 '17 at 6:48
  • Yes...I understand.... you can add a user_meta for "last active time"....and you have to set the "last active time" for every execution. then add this function. [pre]if (is_user_logged_in()) { if ( $last_active_time >= 1800 ) { wp_safe_redirect( site_url('/members.php')); exit; } } [/pre] Apr 1 '17 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.