ButlerBlog

chad butler's weblog

  • About
  • Blog
  • WordPress Plugins
  • Contact
Home / Web / WordPress / Show menu based on WordPress login status

Show menu based on WordPress login status

By Chad Butler 59 Comments

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.

  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest

Filed Under: WordPress, WP-Members Tagged With: development, tips, tutorials, WordPress, WP-Members

About Chad Butler

Chad Butler is a freelance writer and web developer. He has developed several popular WordPress plugins and has written for forbes.com, sfomag.com, and investopedia.com.

Get More Content Like This

I will never share your information. No spam. No junk. No kidding. Unsubscribe anytime.

Recent Posts:

  • 5 Things You Need To
    Know About Cybercrime For 2018
  • 9 Landing Page Goofs That
    Make You Lose Business [Infographic]
  • 5 reasons I use WP-Members for building membership sites
  • The Risk of Relying on Free Plugins
  • Creativity Extortion Part 2 – Turning
    Negative into Positive as a Freelancer
  • Creativity Extortion Working as a
    Freelance Designer, Developer, or Writer
  • New StudioPress Sites Offers Simplicity
    in a Powerful Hosted Package
  • The Premium Support Business Model
  • Word-based Passwords Plugin for WordPress
  • A Great Way to Get Free Gold

Archives

  • About
  • Blog
  • Archive
  • Contact

Site powered by WordPress, running on the Genesis Framework from StudioPress.

Unless otherwise noted, content on this site is © 2006-2019 ButlerBlog and may not be reproduced without express written permission from the author.

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.