• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Add Login/Logout menu items conditionally to Primary nav menu

Last Updated on September 13, 2019 Favorited: 0 times

add_filter( 'wp_nav_menu_items', 'genesiskit_conditional_nav_items', 10, 2 );
/**
 * Filter menu items to append Logout and Login links to Primary Nav for logged in and non logged in users respectively.
 *
 * @param string   $menu HTML string of list items.
 * @param stdClass $args Menu arguments.
 *
 * @return string Amended HTML string of list items.
 */
function genesiskit_conditional_nav_items( $menu, $args ) {

    if ( 'primary' !== $args->theme_location ) {
        return $menu;
    }

    if ( is_user_logged_in() ) {
        $menu .= '<li class="menu-item logout"><a href="' . wp_logout_url() . '">Logout</a></li>';
    } else {
        $menu .= '<li class="menu-item login"><a href="' . wp_login_url() . '">Login</a></li>';
    }

    return $menu;

}

Source: https://gist.github.com/nickcernis/02086c75ee9aa19447e0

Categories: Free Content, Snippets

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service