Sorry to ask such a basic question, I've looked at other questions and not found an answer. I've customized some public pages with their own header-public.php, and I'm doing a basic login check for all other pages right at the beginning of the page (line 1 in header.php). The intent is to redirect unlogged users to a reception page where they can get help, log in, etc.
It's just that a) it doesn't redirect, and b) adding the 'exit;' as recommended breaks the rest of the page too. I tried abstracting this into a function in functions.php and that broke functions.php, so now Idunnowhatsgoingon.
Here's the code:
if (is_user_logged_in())
{
echo 'loggedin';
}
else
{
echo network_home_url( '/receptionist' );
wp_redirect( network_home_url( '/receptionist' ) );
exit;
}
The sanity checks work, and echoing the 'network_home_url' works too - I can copy and paste the url in and it brings the page up fine.
Would anyone have any advice about this? Sure looks to me like it should be working :\
Edit: per Hansy's answer and my response, here's the current code. The exit is commented since it breaks the page, I'll uncomment it when the redirect is working.
if (is_user_logged_in()){
die(); // edit - don't do this per Hansy's response, I misread what it does. My bad.
}
else {
wp_redirect( network_home_url( '/receptionist' ) );
/* exit; */
}
I've also tried: if (!is_user_logged_in()){ wp_redirect( network_home_url( '/receptionist' ) ); /* exit; */ } since that eliminates as much before the redirect as possible.
I'm starting to think there's something wonky with the theme, since This Should Work.
Edit 2: My server has a security package and PHP Safe Mode is On. Is there anything in PHP config that would affect this functionality?
Edit 3: It was the theme. I switched back to 2011 and redid the setup and it Works Perfectly. What in the name of Charles Babbage could the theme dev have done to break a core template tag? Do they not teach separation of presentation, logic and data in school anymore? Jeez. Here's the code that worked, for reference. I'll try Hansy's much upgraded solution too.
if (!is_user_logged_in() )
{
wp_redirect( network_home_url( '/receptionist' ) );
exit;
}