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

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 child-theme’s functions.php file or a custom plugin 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

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

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.

Tags: