wp_redirect not working in header, exit is breaking page

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.

Read More

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;
}

Related posts

Leave a Reply

1 comment

  1. 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;  
        } 
    }