How to add login logout link in wordpress menu

We can add login and logout link using Appearance > Menus and then custom link. But then both links will be shown always, doesn’t matter if you are logged in or out. But if you want to use Login link only for visitors and logout link only for members, this little snippet will help you with that.

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in their. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always.

[php]
function add_login_logout_link($items, $args)
{
if(is_user_logged_in())
{
$newitems = ‘

  • logout
  • ‘;
    $items .= $newitems;
    }
    else
    {
    $newitems = ‘

  • login
  • ‘;
    $items .= $newitems;
    }
    return $items;
    }
    add_filter(‘wp_nav_menu_items’, ‘add_login_logout_link’, 10, 2);
    [/php]

    Enjoy WordPressing :)

    Leave a Reply

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