Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a custom menu that displays a <li></li> when a user is logged in, and not display it when they're not. I have this so far, but it's not working. Does anyone know how to do this properly?

add_filter('wp_nav_menu_items','custom_nav_items',1,2);
function custom_nav_items($menu, $args) {
global $bp;
$args = (array)$args;
if ( $args['theme_location'] != 'primary' )
    return $menu;
$customNav = '<li class="menu-item menu-item-type-post_type menu-item-object-page ss-nav-menu-item-5 ss-nav-menu-item-depth-0 ss-nav-menu-reg ss-nav-menu-mega-alignRight customNav"><a href="'. $bp->loggedin_user->domain .'">'. $bp->loggedin_user->fullname .'</a>
<ul class="sub-menu sub-menu-1">
    <li class="menu-item menu-item-type-custom menu-item-object-custom ss-nav-menu-item-depth-1"><a href="'. $bp->loggedin_user->domain .'">View Profile</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom ss-nav-menu-item-depth-1"><a href="'. $bp->loggedin_user->domain .'profile/edit/">Edit Profile</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom ss-nav-menu-item-depth-1"><a href="'. $bp->loggedin_user->domain .'settings/">Edit Settings</a></li>'
if ( is_user_logged_in() ) {
    '<li class="menu-item menu-item-type-custom menu-item-object-custom ss-nav-menu-item-depth-1"><a href="'. wp_logout_url( bp_get_root_domain() ) .'">Logout</a></li>' }
'<ul>
</li>';
return $menu.$customNav;
}
share|improve this question
    
You're missing a . to connect the strings after your is_user_logged_in(). To be honest: Try to write cleaner code and use an IDE like PHPStorm, Aptana/Eclipse, etc. Those will show you such typos instantly. –  kaiser Sep 26 '12 at 22:59
add comment

2 Answers 2

I do not believe a plugin would be needed for this.

You could add a class to the menu item (enable the class field under Screen Options when you are editing the menu item). Also be sure that you have the body_class() function applied to your body tag. Then you can do:

.your-menu-item-class {
    display: none;
}
.logged-in .your-menu-item-class {
    display: block;
}
share|improve this answer
add comment

There is a plugin available at http://wpsmith.net/my-plugins/wp-custom-menu-filter-plugin/ that has the functionality you are looking for.

You add classes to the menu items to indicate whether they are for logged in users, or unknown users.

share|improve this answer
    
I have used this plugin, works well, simple to use. –  Tim Mar 26 '12 at 18:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.