Show menu based on WordPress login status

In this example, I will show you how to display a different menu for users based on their login status for the WordPress TwentyTen theme.  This process can be adapted to any theme (and using the same principles, could be adapted to use anywhere on your WP site).

Some Prerequisites

This is primarily for those using WP-Members™ who have asked about showing different menu items based on a user’s login status; but the method described is not limited to use with the plug-in.  This will work with any WordPress installation.

This post assumes that the reader is already familiar with creating menus through the WordPress admin panel.  If not, you can familiarize yourself with this process via the WordPress Codex.

It is VERY important to note that you should not make the discussed edits to TwentyTen directly.  You should be using a child theme for any changes, otherwise, when you upgrade WordPress or TwentyTen, your changes will be overwritten.  A tutorial on creating child themes is not the purpose of this post.  If you are not familiar with this process, begin with the WordPress Codex description of Child Themes.

The Process

First, create two menus.  This is done in your WP admin panel under Appearance > Menus.  For this example, I created on called “logged-in” and one called “logged-out”.  It should be intuitive which is which.

For the “logged-in” menu, add the pages and content you want in the menu for users who are logged in.  For the “logged-out” menu, put in the content to display if the user is not logged in.

To implement these menus, you will make a slight change to the header.php file for the TwentyTen theme.  Look for the following line in this file:

<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

Replace that line with the following:

<?php
if( is_user_logged_in() ) {
	$menu = 'logged-in';
} else {
	$menu = 'logged-out';
}

wp_nav_menu( array( 'menu' => $menu, 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
?>

Basically, we are just adding another parameter for wp_nav_menu to determine which menu to load.  We are using a variable $menu that will provide the menu name depending on the user’s login status.

It’s that simple.

For a related discussion on how to display different elements in the Loop area of your theme, see this post: Blocking Content in a Custom Template.

About Chad

Chad Butler is both a freelance writer and web developer. He has developed several popular WordPress plugins and his writing has appeared on forbes.com, sfomag.com, and investopedia.com.
This entry was posted in WordPress, WP-Members and tagged , , , , . Bookmark the permalink.

Related Posts:

6 Responses to Show menu based on WordPress login status

  1. Franc says:

    Hello,

    I have a small problem that I can’t figure out how to fix. At the end of each post there’s the Leave a reply option that says you must be logged in to leave a reply. Which is ok since only registered users are allowed to post comments.
    The problem is that the link leads to a WordPress lookin login form and not the one I created in WP Members. I tried various settings in the WP dashboard but I can’t convience the redirect to go to the login/register form.
    So how can I change this redirect part or just insert a plain link to the form? Thank you so much for your help.

    • Chad says:

      Franc – the link you are talking about is put in at the theme level. There are two ways to change this. One is to add some arguments to the comments_form function in your theme’s comments.php file.

      The other would be to create a filter for login_url like:

      function my_login_url( $login_url )
      {
          return 'http://yourdomain.com/your-login-page';
      }
      add_filter( 'login_url', 'my_login_url', 1 );
      

      The nice thing about that method is that it redirects everyone away from the backend login to the login url you specify (not just for comments). The downside is that “everyone” includes you – so you would need to change some habits in logging in and accessing the dashboard.

      • Fanc says:

        Chad thank you very much for your reply. I would like to create a filter login like you suggested. If you would be so kind, could you please tell me exactly where to insert that because I really really don’t want to mess anything up…
        Thanks

        • Chad says:

          Something like that goes into your theme’s functions.php file. You can just tack it on to the end of the file – just note that if there is a closing php delimiter ( ?> ), that you are pasting *inside* of it.

  2. Miguel Alas says:

    Hi Chad! Great plugin!
    I’ve installed the plugin and was looking for a way to redirect the user to any page I choose after login. I mean I have a main page 1 and main page 2. The main page 1 is for not logged user and as soon they login may be redirected to main page 2. How can I make this?

    Thanks in advanced!!

    • Chad says:

      You could set up one page and separate the content based on login status using shortcodes. The ‘status’ shortcode will display different content based on user login status:

      [wp-members status="out"]This is content that will display to users who are NOT logged in.[/wp-members]

      [wp-members status="in"]This is content that will display to users who ARE logged in.[/wp-members]

      The caveat is that you do not get the benefit of inline login and registration (although if you have the login widget on the sidebar, that will still be active).

Leave a Reply

Your email address will not be published. Required fields are marked *.
Comments are moderated. Please submit only once.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>