Skip to content

trepmal.com

  • Home
  • Plugins
  • Github
  • Me on WP.org
  • Donate

Categories

  • WordPress Bits (149)
  • Uncategorized (11)
  • Scripts (5)
  • Tidbits (5)
  • Fun (3)

Tags

  • wordpress (30)
  • administration (16)
  • plugin (9)
  • free download (8)
  • functions.php (8)

Year

  • 2018 (8)
  • 2016 (2)
  • 2015 (4)
  • 2014 (19)
  • 2013 (11)

Filter Hook: wp_nav_menu_items

Add Log In/Out Links to Custom Menu

Sometimes you want to add a log in/out link (depending on whether the current user is logged in or not) in your main menu. Here’s a simple way to add them to the end.

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
    if (is_user_logged_in()) {
        $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    }
    else {
        $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    }
    return $items;
}
Posted on April 30, 2011March 1, 2012Categories WordPress BitsTags custom menu, log in, log in/outLeave a comment on Add Log In/Out Links to Custom Menu
Proudly powered by WordPress