Add login/logout button in menu

One of many WordPress’ features that we love is its easy way of registering new users. Controlling different user roles on the popular platform couldn’t be easier; there are already registered roles which you ca assign and you can even create numerous custom user roles for the need of your site.

While registering is fast and easy, taking care of your users can be a hard work. But you need to make everything in your power to make their experience on the website as good as possible.

One of many things you could do for them is to enable an easy way of logging in and logging out from your site. To make login/logout links always in reach of users’ hand, you can put those links right in your main menu. First thing, having those links in menu is very practical and secondly, your users have probably grown accustomed to the feature from various other applications which use the same way of logging in and out.

In this case, there’s no need in grabbing another plugin. A simple WordPress function which we’re going to show you in this article can do a great job for you. It will automatically place the correct link in your menu. That means that while user is logged out, he will get the “log in” link and vice versa. Neither you nor the user will have to worry about login buttons again.

If you’re ready to install this simple yet neat login feature into the menu, here’s what you’ll have to do:

  1. Open functions.php
  2. Copy and paste the following:
  3. function add_login_logout_link($items, $args) {
    $loginoutlink = wp_loginout('index.php', false);
    $items .= '<li>'. $loginoutlink .'</li>';
    return $items;
    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
  4. Save changes

As with the most of our snippets, you’re done simply by copying and pasting the function into the file. Once you’ve done that, you can go on and reload the homepage. Since you’re still logged in, you will get the “log out” link on the last spot of your main menu. Be sure to check out the entire function so try logging out and back in to assure yourself everything’s ok.

Now that you’re done with this little feature, don’t forget that you can easily redirect users to another page after successful login or even show content only to logged in or logged out users. Want your users to be able to stay logged in forever? You can do that.

Also, now that you have customized menu, you might be interested in showing it anywhere on the site with by providing a custom shortcode.

We love customizing WordPress with little snippets like this one, how about you?


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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