Skip to content

Instantly share code, notes, and snippets.

@waylay
Last active September 1, 2019 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylay/9d371857b00bcc67f8f688da188de31f to your computer and use it in GitHub Desktop.
Save waylay/9d371857b00bcc67f8f688da188de31f to your computer and use it in GitHub Desktop.
Add Login/Username/Logout(as submenu) to main navigation
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