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.

Before the update to WP 3.6 I could add this code in the header.php of the theme, which made it possible to have another menu when logged in:

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

        wp_nav_menu( array(
            'menu'              => $menu,
            'theme_location'    => 'menu',
            'sort_column'       => 'menu_order',
            'menu_class'        => 'sf-menu',
            'fallback_cb'       => 'default_menu'
        )); ?>

I have made the two menus called "logged-in" and "logged-out" in the WP admin panel.

But after the update, this doesn't work anymore. How can I change the menu when logged in in the correct way?

(PS: The theme is WPExplorer's Adapt theme.)

Note:

Actually, all I need is to change one specific button in the menu, so it is different when the user is logged in - in that case that menu button will be a "Member profile" button instead of a "Log in" button. If that can be done without changing the entire menu, that would be much better.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Actually, all I need is to change one specific button in the menu, so it is different when the user is logged in - in that case that menu button will be a "Member profile" button instead of a "Log in" button

Yes, you can use only one menu, assuming its slug is 'my_menu' do not add to it member profile nor login url, instead use wp_nav_menu_{$menu_slug}_items filter:

// 'my_menu' in filter name is the slug of the menu
add_filter('wp_nav_menu_my_menu_items', 'menu_add_admin_buttons', 20, 2); 

function menu_add_admin_buttons( $items, $args ) {
  $btn_format = '<li><a href="%s">%s</a></li>';
  if ( is_user_logged_in() ) {
    $btn = sprintf($btn_format, admin_url('profile.php'), __('Your Profile') );
  } else {
    $btn = sprintf($btn_format, wp_login_url(), __('Log In') );
  }
  return $items . $btn;
}

$items are the current html for the current menu items, a string containg a series of <li><a ... </a></li>.

The slug of the menu is equal to sanitize_title($menu_name) i.e. it removes all special chars, converted accented chars with non accented ones and convert spaces to -.

E.g. if the name is "Main Menu" the slug is "main-menu".

The name your menu is called is what you write in wp backend when you create menu, see image below.

enter image description here

Of course is possible use sanitize_title to create the filter dinamically from name, e.g.

$menu_filter = 'wp_nav_menu_' . sanitize_title("Main Menu") . '_items';
add_filter($menu_filter, 'menu_add_admin_buttons', 20, 2); 
share|improve this answer
    
Hmm, this doesn't seem to work... The menu slug will be the manually set menu name, right? What does the first argument $items do? –  Steeven Aug 31 '13 at 5:26
    
It seems the problem is different. My child theme doesn't work - it simply isn't used/added to the main theme. The functions.php as well as the header.php file in the child theme don't change anything. That is of course the cause. So that must be sorted out first... –  Steeven Aug 31 '13 at 5:48
    
@Steeven Tested, it works perfectly. I've added some explainations to answer. –  G. M. Aug 31 '13 at 5:54
1  
@Steeven to test child theme try to insert die('Test'); in your child theme functions.php. If the white screen with the only word 'Test' doesn't appear on site open you have a problem with the style.css file of your child theme, or the child theme is not the one activated in the backend. –  G. M. Aug 31 '13 at 5:59
    
Child theme working again! And your code works perfect. Thanks alot for answer and the extra info. –  Steeven Aug 31 '13 at 6:05
add comment

Try changing the theme location as well. How are you registering the menus? You could try this..

functions.php

<?php
function register_menus() {
    register_nav_menus(array(
        'logged-in'  => 'Logged In Nav',
        'logged-out' => 'Logged Out Nav'
    ));
}

add_action( 'init', 'register_menus' );
?>

header.php

<?php
if ( is_user_logged_in() ) {
    $menu      = 'logged-in';
    $menu_name = 'Logged In Nav';
} else {
    $menu      = 'logged-out';
    $menu_name = 'Logged Out Nav';
} ?>

<nav role="navigation">
    <?php
    wp_nav_menu( array(
        'menu'              => $menu_name,
        'theme_location'    => $menu,
        'sort_column'       => 'menu_order',
        'menu_class'        => 'sf-menu',
        'fallback_cb'       => 'default_menu'
    ));
    ?>
</nav>

Not tested, just an idea.

Also if you want to try it with one menu like you mentioned, you could use the body classes to target those specific links.

<body <?php body_class(); ?>>

This will add classes to the body and if a user is logged in it will add a 'logged-in' class. There isn't a logged-out class so you could hide the one you want to show only to logged in users then display it when they are logged in.

.menu-item-101 { /* hide for logged out users */
    display: none;
}

.logged-in .menu-item-101 { /* show for logged in users */
    display: block;
}

.logged-in .menu-item-102 { /* only show to logged out users */
    display: none;
}
share|improve this answer
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.