How To Display Conditional Login Or Logout Links In WordPress
In this example, we’re adding custom conditional links to a mega menu depending on whether the user is logged in or not, and adding them to the end of our navigation menu.
/////////// // Add conditional links to nav menu /////////// add_filter( 'wp_nav_menu_items', 'uwp_menu_last_to_nav_menu', 10, 2 ); function uwp_menu_last_to_nav_menu( $items, $args ) { if ( is_user_logged_in() ) { $last = '<li class="mega-menu-item mega-menu-item-type-custom mega-menu-item-object-custom mega-align-bottom-left mega-menu-flyout"><a class="mega-menu-link" href="'. esc_url( home_url( '/' ) ). get_the_author_link() .'">My Profile</a></li>'; $last .= '<li class="mega-menu-item mega-menu-item-type-custom mega-menu-item-object-custom mega-align-bottom-left mega-menu-flyout"><a class="mega-menu-link" href="'. esc_url( home_url( '/' ) ). 'wp-login.php?action=logout">Log Out</a></li>'; $lastitems = $items . $last; } if ( !is_user_logged_in() ) { $last = '<li class="mega-menu-item mega-menu-item-type-custom mega-menu-item-object-custom mega-align-bottom-left mega-menu-flyout"><a class="mega-menu-link" href="'. esc_url( home_url( '/' ) ). 'login">Login</a></li>'; $lastitems = $items . $last; } return $lastitems; }