How to Add a Login/Logout Link to Your WordPress Menu


Almost nothing is more frustrating for a user than searching around for a login link in order to get to important content.

The following method found at Vandweerd.com will automatically detect whether a user is logged in or not and put a login or a logout link right on your menu bar.

*** Please note, this method only works when you are using WordPress’ custom menus. (The menu function available in the admin section: Appearance > Menus.)

Add Code to Your Functions File

You will need to add a bit of code to your functions file for this. But after copying and pasting this code, you’re finished.

Go to Appearance > Editor > Theme Functions (functions.php). Place the following code in the bottom your functions file and hit “Update File.”

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
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;
}

The Result

Keep in mind that these links will appear wherever you put your custom menus – be they at the top of your page, in your sidebar, or anywhere else.

Photo: login icon from BigStock