How to Add a Login/Logout Link to WordPress Menu

If you allow users to register on your WordPress website/blog, then its really important to add a login/logout link to your website.

In this post, I will show you how to add a login / logout link to the your WordPress navigation menu. This code snippet automatically detects whether the user is logged in or not and display the login or logout link accordingly.

//Add Login/Logout Links To WordPress Navigation Menu
add_filter( 'wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link( $items, $args )
{
    if($args->theme_location == 'primary')
    {
        if(is_user_logged_in())
        {
            $items .= '<li><a href="'. wp_logout_url() .'">Logout</a></li>';
        } else {
            $items .= '<li><a href="'. wp_login_url() .'">Login</a></li>';
        }
    }

    return $items;
}

Note: Change primary to your own theme location.

Useful Read: Applying Categories and Tags to WordPress Media Attachments

0 Comments

Leave a Comment.