I'm running a closed website for registered users only.

For doing so I check in if user is logged in

// no non-authenticated users allowed
if( !is_user_logged_in() ) {

But sometimes It just logged me out, and I could not understand where to seek the problem. it happened ocassionally and so it was really hard to debug.

Now: with different function I found that if( !is_user_logged_in() ) returns true on 404 page.

I'm still debugging but I want to hear your opinion, is this normal or intended? Could you confirm if this is same with your installaton? I'm running latest version of Wordpress


still cant undestand, in footer.php

if(!is_user_logged_in()) {
    echo '123';

echos 123 but my function does not redirect.. but its still strange and have problem with another plugin because of this.

plugin looks like this:

function bpln_store_ids(){
 <div id="bpln-notification-ids" style="display:none;">
 <?php echo join(",",bpln_get_all_notification_ids(bp_loggedin_user_id()));?>

since check returns true, the actual content will never be displayed

  • Where are you doing your check? You can only do a redirect before any HTML is outputted. If you call for a redirect after the content has started being sent, it will be ignored. – Seamus Leahy Jan 29 '14 at 19:18
  • I use this in header, before the doctype. but problem rises in footer.php. I updated my question. This is still a problem. can you confirm if this is same everywhere? I also think this is a bug – CBeTJlu4ok Jan 29 '14 at 19:23
  • 1
    I'm not having any issues with is_user_logged_in() reporting the wrong thing on the 404 page - even in the footer. You may have a plugin that is changing the state of the current user. You need to disable plugins and non-default themes to determine if it is one of those first. If the issue exists with a stock WordPress install, then it is a WordPress issue, otherwise it is has to do with an add-on. – Seamus Leahy Jan 29 '14 at 19:42
  • Is there a link we can visit to see it in action? – eteich Jan 29 '14 at 19:44
  • 2
    You shouldn't use redirect in the header.php file, that's usually too late, the headers are then already sent. So hook it earlier, for example the template_redirect hook. – birgire Jan 29 '14 at 19:56

Please try this for your redirect:

add_action( 'template_redirect', function(){

    // no non-authenticated users allowed
    if( ! is_user_logged_in() )
        wp_redirect( home_url( '/wp-login.php' ), 302 );

to allow only logged in users to view your site.

It's generally too late to use redirect directly in the header.php file, so it's better to use an hook that fires before http headers are sent, like the template_redirect hook. It's also important that template_redirect is not activated on the wp-login.php page.

  • 3
    Here go the upvotes. How easy was that? ;) – kaiser Jan 30 '14 at 2:31
  • I wont mark this as a correct answer, since question is different, but this is interesting and might be my other problem, ill try this – CBeTJlu4ok Feb 1 '14 at 9:05
  • Okay, tried this but does not work, when I'm logged out, I still can view the site. maybe template_redirect is a wrong action? – CBeTJlu4ok Feb 1 '14 at 9:08
  • That's strange, I've tested it with success on a vanilla install. Maybe you are using a plugin/theme that's interfering? Maybe you got some cookie problems, did you try this in another browser or clear cache? You can try another earlier hook like wp, but if you want to use hooks like init you have to change the above code to avoid infinite loop. What's the output of home_url( '/wp-login.php' )? What happens if you replace wp_redirect() with die( 'Stop!' )? Did you add the code snippet into your functions.php file? – birgire Feb 1 '14 at 16:52

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.