# Log In / Log Out Item in the Menu

> Appends a "Log in" link to your main menu for visitors and a "Log out" link for logged-in users, returning them to the same page.

- Type: PHP
- Runs: Run everywhere
- Category: [Frontend & Design](https://wpide.com/library/category/frontend/)
- Tags: menu, login, logout, navigation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/frontend-login-logout-menu-item/

## Code

```php
add_filter( 'wp_nav_menu_items', function ( $items, $args ) {
    $location = 'primary'; // Theme menu location.

    if ( ! isset( $args->theme_location ) || $args->theme_location !== $location ) {
        return $items;
    }

    $current = home_url( add_query_arg( array() ) );

    if ( is_user_logged_in() ) {
        $items .= '<li class="menu-item wpide-menu-logout"><a href="' . esc_url( wp_logout_url( $current ) ) . '">Log out</a></li>';
    } else {
        $items .= '<li class="menu-item wpide-menu-login"><a href="' . esc_url( wp_login_url( $current ) ) . '">Log in</a></li>';
    }

    return $items;
}, 10, 2 );
```

## Notes

Set $location to your theme's menu location slug. Works with classic menus.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Log In / Log Out Item in the Menu" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
