I'm making a site with WordPress as the CMS (installed on our server, not WordPress.com). I want the account page to redirect to the login page if the user isn't logged in. There's a custom template for the account page (account.php) and at the top I've placed this code:
if(!is_user_logged_in()){
wp_redirect('www.mypage.com/login');
die();
}
When I update and save the account.php page, I go into the WordPress dashboard and update the page there too. When I go to view the account page (either by refreshing the page or clicking "view page") it usually redirects the first time to the login page. Yay! All seems well, except when I click the account link, I expect it to take me to the login page as I'm still not logged in, but it takes me to the account page anyway... I've tried in InCognito mode to be sure I'm not logged in. I put an echo inside that if statement, and it reaches it just fine, so I know that it's correctly detecting that I'm not logged in.
Does anyone know why it's redirecting on first load but not again when navigating back to that page? How can I make it so it's impossible to view account.php until logged in?