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.

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.
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.
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
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.Hi, is it possible to adapt it to the Simploblack theme?, It would be great!… thanks
The plugin’s interface is mostly CSS driven, so it can be adapted to most any theme:
http://butlerblog.com/2011/07/25/customize-the-wp-members-stylesheet/
http://butlerblog.com/2012/02/20/loading-custom-stylesheets-with-wp_enqueue_style/
Thank you so much for your help, Chad.
One more question, I’m trying to show the registration form, which should include only the fields I selected on the Setting’s Field section.
I don’t know what URL to use in the ‘Register Page URL’ field in order to show the Register link.
thanks again ;o)
That’s only if you set up a specific register page and you want to have the link to it show at the bottom of the login form. If you set up a specific registration page with the shortcode [wp-members page="register"], you would put the URL of that page in there.
Thank you, thank you very much Chad. You rock!!
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!!
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).
Hi Chad,
I’ve followed the instructions in this post but I have a different template (not twenty ten or eleven) in which a custom menu is already used for the register/login links that is put into the header area on the widgets screen of WP admin area. Before my header.php had this code( are replaced by [ and ] in the hopes that you’ll get to see the code):
[nav class="primary"]
[?php wp_nav_menu( array(
'container' =] ‘ul’,
‘menu_class’ =] ‘sf-menu’,
‘menu_id’ =] ‘topnav’,
‘depth’ =] 0,
‘theme_location’ =] ‘header_menu’
));
?]
[/nav][!--.primary--]
I added the menu parameter like you said, but it seems that in this theme the header widget is showing the custom menu, which appears above the regular header menu:
[div id="header-widget"]
[?php if ( ! dynamic_sidebar( 'Header' ) ) : ?]
[!-- Widgetized Header --]
[?php endif ?]
[/div]
So I’m still seeing my static custom header (in the header-widget div) and not the logged-in or logged-out ones. Do you know how I can change which widget is shown based on logged in status?
Thanks,
Robert
It all boils down to using is_user_logged_in. You might have two completely different headers for logged in/logged out users.
if( is_user_logged_in() ) { // the user is logged in. // any code for the logged // in user goes in here. } else { // the user is not logged in. // anything you show to // logged out users goes here. }Also, if you are strictly limiting an element to just logged in users and there is nothing in there that needs to render for the logged out state, you can just drop the else{ … } part of it.
Thanks for this really effective tip. I customized it slightly to work with my client’s BuddyPress site and it works perfectly!
I got this to work but is there a way to make the code check for role the user is? I mean can I have a menu for Subscriber / Contributor / Author? I want each role to have access to different parts of the site.
Since WordPress transitioned from a User Level based system to Roles based system, it is best to query the user capability by using something like current_user_can() and use a capability appropriate to a given role in your situation (as roles and capabilities may vary from install to install).
Hi Chad,
I like the ease of use of this plugin but I want to know if you’ve replaced any of the following functions in your plugin. I’ve been trying to auto-login after creating a new account, but it seems that the cookies are not being set correctly. I’m using this code:
wp_set_current_user($user_id, $username);
wp_set_auth_cookie($user_id,true);
do_action(‘wp_signon’, $username);
If I check is_user_logged_in() it returns true, but when I redirect to the index page or open a new tab my new user is not logged in. Is there a different way for me to auto-login users with your WP-Members plugin installed?
Thanks,
Robert
I think you might want to use wp_signon directly, rather than via do_action. Also, wp_signon needs credentials passed as an array. (see: http://codex.wordpress.org/Function_Reference/wp_signon)
Hi Chad Im Very New to wordpress,i have wp members installed and working great,Now what I would like to do is fix it so i will have separate pages for logged in users and not logged in users.If any code is involved please tell me to insert it..Thanks in advance..
I would suggest reviewing the Quick Start Guide and the Users Guide, both of which can be found at http://butlerblog.com/wp-members