Last active
September 1, 2019 13:21
-
-
Save waylay/9d371857b00bcc67f8f688da188de31f to your computer and use it in GitHub Desktop.
Add Login/Username/Logout(as submenu) to main navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_login_logout_to_menu( $items, $args ) { | |
if ( $args->theme_location != 'primary' ) { | |
return $items; | |
} | |
if ( !is_user_logged_in() ) { | |
$items .= '<li><a href="'.wp_login_url().'" title="Login">Login</a></li>'; | |
} else { | |
$current_user = wp_get_current_user(); | |
$items .= '<li class="menu-item-has-children"><a href="#">'. $current_user->display_name .'</a><ul class="sub-menu">'; | |
$items .= '<li><a href="'.wp_logout_url( home_url() ).'">Logout</a></li>'; | |
$items .= '</ul></li>'; | |
} | |
return $items; | |
} | |
add_filter( 'wp_nav_menu_items', 'add_login_logout_to_menu', 199, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment