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 using WordPress' built-in menu system to output my Main Menu Structure. I have one sub-menu, and it has two links currently, "Tracking" and "To-Do List". I want the following login greeting to show above those two links:

<?php global $current_user; if ( is_user_logged_in() ) { echo 'Welcome, '.$current_user->first_name.''; } else { echo 'Welcome, visitor'; } ?>
    <font id="spacer">•</font>
<?php add_modal_login_button( $login_text = 'Login', $logout_text = 'Logout', $logout_url = '', $show_admin = false ); ?>

The output looks like this if no one is logged in: Welcome, visitor • Login

How can I show this output above the two sub-menu links?

share|improve this question
1  
The code is correct. Can you please also put the code for the menu/submenu that you have created which includes the above code? –  Chittaranjan Dec 10 '13 at 2:44
    
I'm not creating a custom menu template through code, I'm going through WordPress' menu interface: archondigital.com/wp-content/uploads/wordpress-menu.gif –  eclipsis Dec 10 '13 at 17:57
    
If I am not wrong, then you want your code to reflect in the menu system as per user's login status. But I am still not sure how and where you are executing the above code. –  Chittaranjan Dec 10 '13 at 18:11

1 Answer 1

From your code it seems like you are using this modal login plugin http://wordpress.org/plugins/wp-modal-login/ Considering this, I am providing the necessary code to add the menu item to the menu.

You need to use the wp_nav_menu_items hook as below inside functions.php file of your active theme.

// Add the hook to the nav menu items
add_filter( 'wp_nav_menu_items', 'wti_loginout_menu_link', 10, 2 );

function wti_loginout_menu_link( $items, $args ) {
    // Get the global object for user and the modal login class
    global $current_user, $wp_modal_login_class;

    if ( $args->theme_location == 'primary' ) {
        $items .= '<li>';

        if ( is_user_logged_in() ) {
            $items .= 'Welcome, ' . $current_user->user_nicename;
        } else {
            $items .= 'Welcome, visitor';
        }

            // Add the modal menu to the nav menu
        $items .= ' ' . $wp_modal_login_class->modal_login_btn( 'Login', 'Logout', '', false );

        $items .= '</li>';
    }

    return $items;
}

Few things to note:

  • The above code works with primary theme location. You need to change this to your theme location.

  • I am using user_nicename, you can use first_name of the user object as per your code.

The above hook is also discussed here http://www.webtechideas.com/adding-login-logout-link-to-wordpress-menu/

share|improve this answer
    
Thank you for the answer Chittaranjan. Could you clarify how I could insert this login info into the 'Account' sub menu? This is what my menu and the sub menu I want to insert the login info into (Account) looks like: i.imgur.com/KUpRybm.gif –  eclipsis Dec 10 '13 at 23:23

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.