WordPress Tip — How To Add Login / Logout Link To WordPress Menu

When we add a menu in WordPress it does not display the Login / Logout link by default. It is a good option to place Login/Logout link in your WordPress navigation menu. This link works in vice-versa fashion, for instance, if the current user is logged in, they will see a Logout link and if there is no logged in or active user the link will change to Login. To include the Login / Logout link in your WordPress menu, place the following code in your custom_functions.php:

function add_login_logout_link($items, $args){
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
} 

This code will add the Login / Logout link to both — primary as well as secondary navigation. But if you want the Login / Logout link to be added only to secondary navigation then you just need to replace the last line of above code with this:

add_filter('wp_nav_menu_secondary_items', 'add_login_logout_link', 10, 2);

How about adding Login / Logout link to your WordPress menu!

Posted In:

UX & Frontend Design, Code Snippets

Comment on WordPress Tip — How To Add Login / Logout Link To WordPress Menu

Your email address will not be published. Required fields are marked *