This is driving me nuts, and I'm sure that as soon as someone points out the answer, I'm going to feel dumb.

I have a website where most pages require login to view. So I am using an if statement to view page content if a user is logged in, or view a login form if not.

If I put this in a page template, everything works just fine:

<?php get_header(); ?>

<?php if(is_user_logged_in()): ?>

    <div id="wrapper">
        <h5><?php the_field('headline'); ?></h5>
        <?php the_field('text'); ?>

<?php else:

    $args = array( 'pagename' => 'request-access' );
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();


endif; ?>

<?php get_footer(); ?>

That's great, but it makes more sense to put the if statement in the header so I don't have to copy it onto every single page template. But if I put the exact same if statement:

<?php if(is_user_logged_in()): ?>

at the end of the header.php, I get an error saying "Parse error: syntax error, unexpected $end" in the last line of header.php. So why does this work at the beginning of a template file but not at the end of the header file?

1 Answer 1


Because you ending php file with : and not putting at the end of header endif; php expects it to finish your IF.

In other words - you can not use this in header this way without ending IF. Or you have to create global variable and asign value [lets say true if logged in] to it in header so other php in theme can use it, or you have to use this code in every php you want to restric something:

if(is_user_logged_in()) {
your code for logged in visitors
} else {

your code for guests

  • So the problem is that I'm splitting my if/then statement between files... sounds like I'll need to put the if statement at the beginning of every theme file.
    – Morgan Kay
    Jun 6, 2012 at 19:26
  • yes, you can not split if statement across files. unless you include those files in header by include function, but you not. and for purpose of just showing login box nobody doing like you're trying here, it's bad aprouch. cause guest won't see footer, content and so on, only header part :)
    – Tommixoft
    Jun 6, 2012 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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