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.

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'); ?>
    </div>

<?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();
            the_content();
        endwhile;
    endif;

    wp_reset_postdata();

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

}
share|improve this answer
    
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. –  Gwendydd Jun 6 '12 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 '12 at 19:29

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.