Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

wp_redirect send a header, so echoing anything before it will make the redirect fail. So, in this case you could remove echo network_home_url( '/receptionist' ); and if there isnt any output being sent it will work just fine.

Update: In both cases the issue is that you do an infinite redirect (redirect to that url if not logged in),and commenting the exit does not help, also in the first case,when using die() you kill the page for all logged in users, instead use something like this (added checks if we are on admin so an user can login-register)

global $pagenow;
if ( !preg_match("!wp-admin!",$_SERVER["REQUEST_URI"] ) and !in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ) ) ){
    $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
    $current_url=$scheme . '://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    $network_url=network_home_url( '/receptionist' );
    if (!is_user_logged_in() and $current_url!=$network_url){ 
        wp_redirect( $network_url );  exit;  
    } 
}
share|improve this answer
    
Thanks for the answer @Hansy. I'd first tried without any checks since this was so simple a function, and it didn't work, so I added the echos to see what was happening. I've removed the checks and tried again, and still no luck. See my edit above for the current code. –  Dains Sep 28 '11 at 12:41
    
Updated my answer –  Hansy Schmitt Sep 28 '11 at 13:39
    
Thank you Hansy! I'll try that with the problematic theme and hope it gets by the issue it induces. Thanks for all of your help! –  Dains Sep 28 '11 at 14:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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