# Login / Logout Link Shortcode

> Registers a [login_logout_link] shortcode that shows "Log in" to visitors and "Log out" to logged-in users, returning them to the same page.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: content, shortcode, login, menu
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/login-logout-link-shortcode/

## Code

```php
add_shortcode( 'login_logout_link', function () {
    $current = get_permalink();
    $current = $current ? $current : home_url( '/' );

    if ( is_user_logged_in() ) {
        return '<a class="logout-link" href="' . esc_url( wp_logout_url( $current ) ) . '">Log out</a>';
    }

    return '<a class="login-link" href="' . esc_url( wp_login_url( $current ) ) . '">Log in</a>';
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Login / Logout Link Shortcode" 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/
