Add Conditional Login/out Links To A WordPress Menu

Written by  //  16th October, 2011  //  Tricks & Tips  //  No comments

It’s pretty easy to add a conditional Login/Logout menu item to your WordPress navigation. Here’s how.

Simply pop open your active theme’s functions.php file, scroll to the bottom (that’s always the best place to put custom hacks and modifications in my opinion) and just before the closing php tag paste the following code.

/******************** Login/Logout Links In Menu ********************/

function add_login_logout_link($items, $args)
{
  if(is_user_logged_in())
  {
$newitems = '</pre>
<ul>
	<li><a title="Logout" href="'. wp_logout_url('index.php') .'">Logout</a></li>
</ul>
<pre>

';
    $newitems .= $items;
  }
  else
  {
$newitems = '</pre>
<ul>
	<li><a title="Login" href="'. wp_login_url('index.php') .'">Login</a></li>
</ul>
<pre>
';
    $newitems .= $items;
  }
  return $newitems;
}
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);

About the Author

View all posts by

Leave a Comment

* Copy this password

* Then type or paste it here


comm comm comm