0

How can I conditionally load HTML for the home page based on whether or not the user is logged in?

Currently my theme has an index.php file. In that file I tried to create:

<?php
if(is_user_logged_in()) {
 get_header(); ?>
<div id="primary" class="content-area">
    <div class="primary-inner">
        <div id="content" class="site-content content-list" role="main">
        <?php 
        if ( have_posts() ) : 
            while ( have_posts() ) : the_post(); 
                get_template_part( 'content', get_post_format() ); 
            endwhile;
            dw_minion_content_nav( 'nav-below' ); 
        else : 
            get_template_part( 'no-results', 'index' ); 
        endif; 
        ?>
        </div>
    </div>
</div>
<?php get_sidebar('secondary'); ?>
<?php get_footer(); ?>
<?php } else { ?>
    // load plain ol' static boring HTML here with a login widget area.
<?php
} ?>

However, this doesn't work. In fact, no matter what I change in the index.php file, nothing else changes. I can even delete the file and it works fine!!

I can see that I have a header.php file. If I modify that file, then I can see changes appear. But, that means I'd have to modify that, footer.php, page.php, content.php etc etc, ultimately having multiple files that have conditional logic in them.

I'm new to developing in Wordpress. There has to be a better way to do this right?

If the person is not logged in, a completely different home page template / design should show up that has one single widget area where the WP login form (bp login widget) shows up. Once that person is logged in, the normal home page with default template should appear.

In Reading, a static page is set and it has a template of "Default Template."

3 Answers 3

3

First check that there isn't a page set as a front page in "Reading Setting". If that is set, index.php is not used as the home page, but the template of the page you set as front page.

In that case, you need to find that template and add you conditional is there.

<?php
if(is_user_logged_in()) {
 get_header(); ?>
<div id="primary" class="content-area">
    <div class="primary-inner">
        <div id="content" class="site-content content-list" role="main">
        <?php 
        if ( have_posts() ) : 
            while ( have_posts() ) : the_post(); 
                get_template_part( 'content', get_post_format() ); 
            endwhile;
            dw_minion_content_nav( 'nav-below' ); 
        else : 
            get_template_part( 'no-results', 'index' ); 
        endif; 
        ?>
        </div>
    </div>
</div>
<?php get_sidebar('secondary'); ?>
<?php get_footer(); ?>
<?php } else { ?>
    YOU CAN CREATE A PAGE TEMPLATE HERE THAT WILL SHOW IF USER IS NOT LOGGED IN
     YOU CAN ALSO ADD A LOGIN WIDGET HERE. 
<?php
} ?>
2
  • The first part of your answer is leading me on the right track. Indeed my static was set per the instructions in a plugin I installed. Now that I've turned it off, I'm able to manipulate page.php and front-page.php. However the 2nd half is a little different than what I'm looking for. From the opening doctype tag to the closing </html> tag, the entire html structure has to change based on the is logged in condition. Perhaps a better question is how to switch between templates for the home page based on if the user is logged in or not? I think your solution will keep same header / footer, yes?
    – user658182
    Commented Apr 16, 2014 at 12:37
  • @user658182 Check my update. Commented Apr 16, 2014 at 12:56
1

You might wanna take a look at this: https://codex.wordpress.org/Template_Hierarchy

Your index.php is typically the last file that Wordpress will look for when loading any page. In the case of the homepage, it will first look for front-page.php then home.php (if your home page is set to show latest posts, and not a static page). If you're changing the index.php and nothing is happening, then it means another template is being used higher up in the hierarchy.

2
  • I think that's where some of my confusion is. Static indeed is set to a specific page. But, that page uses the "Default Template" selection. In my template files, I do not have home.php or front-page.php, however I do have index.php and page.php Do you think the question is better phrased, "How can I switch the home page template based on whether or not a person is logged in?" Ultimately the entire structure (HEAD, TITLE, BODY, etc) has to be completely different based on the condition. But, even editing page.php isn't working. I wonder if a plugin can override these?
    – user658182
    Commented Apr 16, 2014 at 12:10
  • 1
    hmm I think I'm onto something with the plugin route. There was a plugin that provided a page and the requirement is to set that in static Reading setting. Once I turned that plugin off, page.php changes showed up np.
    – user658182
    Commented Apr 16, 2014 at 12:15
0

You could add a widget with the if ( is_user_logged_in() ) { conditional tag and add the HTML to a text widget.

Or you could create a different home page template and use this code in your functions file:

add_filter( 'template_include', 'wpsites_conditional_home_page_template', 99 );

function wpsites_conditional_home_page_template( $template ) {

    if ( is_user_logged_in() ) {
        $new_template = locate_template( array( 'alt-home-page-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

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.