Add Logout link to navigation menu

Posted by Prem Tiwari | Updated on

Add Logout link to navigation menu. You can achieve this using the wp_nav_menu_items hook. Let’s have a look at the following piece of code which shows the login/logout link on the primary menu location.

This is what we have implemented in the above example.

  1. First added a filter for wp_nav_menu_items hook and attached a function to it.
  2. After checking for primary theme location, we have checked whether user is logged in or not.
  3. If logged in, we have showed the Log Out link otherwise the Log In link.
  4. We have passed the permalink of the currently viewing page to the login url so that user will be redirected to the current page after successful login.
  5. We have used the class="right" to the above code to meet your requirement.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers