You can add this code to your active theme’s functions.php file. I would recommend you to use Child theme. The code checks if the user is logged in or not and if the current menu is the primary menu. Then, it adds the appropriate login or logout link to the menu items. The wp_login_url() and wp_logout_url() functions generate the URLs for the login and logout pages respectively. The __() function is used for translation purposes. You can modify the code to match your specific theme and menu requirements.

function add_login_logout_link_to_menu( $items, $args ) {
    if (is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. wp_logout_url() .'">'. __("Logout") .'</a></li>';
    } elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. wp_login_url() .'">'. __("Login") .'</a></li>';
    return $items;
add_filter( 'wp_nav_menu_items', 'add_login_logout_link_to_menu', 10, 2 );
Spread the love