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 am using twenty twelve with a child theme. I have a link in my primary nav menu where I have an link for “My Account” which has a sub menu with a “Login/Register” link that redirects to a modified wp-login.php. Is it possible for me to turn the “Login/Register” link into a “Logout” link when a user is logged in? If it is possible, would you know how I could accomplish this, or point me in the right direction?

Everything I've found online is adding a login/logout link to the primary menu. I would like to leave the menu intact since the "My Account" sub menu will have more links added to it.

I'm not sure if this is relevant but, I created the sub menu using Wordpress menus and created a custom link to redirect it to wp-login.php

Thank you

share|improve this question
    
Please show your code. –  kaiser May 20 '13 at 0:50

1 Answer 1

Here's how I managed to set it up for one client. The code (which should go into the theme's functions.php) assumes there's an existing menu item titled 'Login'. Works fine but may not be the best way, so critique is welcome.

function wpse0520_modify_login_menu_item( $item ) {

    if(isset($item->title)){
        if( $item->title =='Login' ) {
            if(is_user_logged_in()){
                $item->url = wp_logout_url( get_permalink() );
                $item->title = 'Logout';
            } else {
                $item->url = site_url( 'login' );
            }
        } elseif ( $item->title =='Logout' ) {
            if(!is_user_logged_in()){
                $item->url = site_url( 'login' );
                $item->title = 'Login';
            } else {
                $item->url = wp_logout_url( get_permalink() );
            }
        }
    }
    return $item;
}
add_filter( 'wp_setup_nav_menu_item', 'wpse0520_modify_login_menu_item' );
share|improve this answer
    
This is a perfect solution - just plug in an go. –  Sri Aug 19 '13 at 19:37

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.