Logged in and logged out content tutorial
This week’s tutorial is all about showing or hiding content depending if a user is logged in or not. A few weeks ago in the forums The iconBLock Ltd was tweaking the Social theme to not show some navigation depending on if a user was logged in or not. As a result of this the following tutorial was born to show people how to do it for themselves.
We’re going to dive right in with the video and after will be the written tutorial with code.
The important function we’re going to be using that will be core to all of the examples is the function:
<?php if ( is_user_logged_in() ) : ?> <?php else : ?> <?php endif; ?>
Using this simple function and either the if / else or just an if we can hide or show content depending on whether a user is logged in or not. It’s that easy.
Showing different widgets depending on if signed in or not
First of all we’re going to use the existing code so we want open the file sidebar.php then we want to find the first if statement at line 8:
<?php if ( is_user_logged_in() ) : ?>
Add a widget area after so that becomes:
<?php if ( is_user_logged_in() ) : ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar-Loggedin') ) : ?> <?php endif; ?>
Next at the else statement at line 31 find:
<?php else : ?>
Add a widget area to that for the logged out content so it becomes:
<?php else : ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar-Loggedout') ) : ?> <?php endif; ?>
Save that file and lets move onto functions.php.
After line 16 where the register_sidebar is add the following:
register_sidebars( 1, array( 'name' => 'Sidebar-Loggedin', 'before_widget' => '<div id="%1$s">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' ) ); register_sidebars( 1, array( 'name' => 'Sidebar-Loggedout', 'before_widget' => '<div id="%1$s">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' ) );
Save that file and then go into appearance > widgets and you can add some widgets to the loggedin and loggedout widget areas.
There you go that is how you can show different widgets depending on if a user is logged in or not.
Showing a message to logged in users only
Finally we’re going to add a message that will only show to our logged in users. First, open up header.php and find the following line 44:
<h1 id="logo"><a href="<?php echo site_url() ?>" title="<?php _e( 'Home', 'buddypress' ) ?>"><?php bp_site_name() ?></a></h1>
After that, lets add the phrase we want to show to logged in users only and use the if user logged in check so we have the following:
<h1 id="logo"><a href="<?php echo site_url() ?>" title="<?php _e( 'Home', 'buddypress' ) ?>"><?php bp_site_name() ?></a> <?php if ( is_user_logged_in() ) : ?> Welcome member <?php endif; ?> </h1>
Save the file and refresh. There you go, you’ve now added a logged in message, log out and you can see the message for logged out users.
As you can see with this simple check you can easily show or hide content it’s up to you what you do with it.
Here are some great links around these topics if you wish to read a little deeper:
http://codex.wordpress.org/Function_Reference/register_sidebars
http://codex.wordpress.org/Function_Reference/register_sidebar
Power tip
Want to do a quick not logged in check? How about this:
<?php if (!is_user_logged_in()) { ?> // code here }
We hope you enjoyed this tutorial and let us know if you’d like any other topic covered.
Comments : 2
Leave a Reply
Hi Tammie :-)
I am using the Buddy Social theme which I just LOVE. I would like to know how to hide the Activity menu option until the user logs in.
Any help you can give is GREATLY appreciated.
Thanks,
Paul Podczervinski
@Dainis: Possibly page templates would work or you may have to look into a plugin for this but I am not aware of one that may work or not.