1

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

1
  • Please show your code.
    – kaiser
    May 20, 2013 at 0:50

1 Answer 1

3

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' );
1
  • This is a perfect solution - just plug in an go.
    – Sri
    Aug 19, 2013 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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