# Login Form Shortcode

> Adds a [wpide_login_form] shortcode that shows a login form on any page, or a "you are logged in" message with a logout link.

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

## Code

```php
add_shortcode( 'wpide_login_form', function ( $atts ) {
    $atts = shortcode_atts( array( 'redirect' => '' ), $atts, 'wpide_login_form' );

    $current  = get_permalink() ? get_permalink() : home_url( '/' );
    $redirect = $atts['redirect'] ? wp_validate_redirect( $atts['redirect'], $current ) : $current;

    if ( is_user_logged_in() ) {
        return sprintf(
            '<p class="wpide-logged-in">You are logged in as %1$s. <a href="%2$s">Log out</a></p>',
            esc_html( wp_get_current_user()->display_name ),
            esc_url( wp_logout_url( $current ) )
        );
    }

    $html  = '<div class="wpide-login-form">';
    $html .= isset( $_GET['login'] ) && 'failed' === $_GET['login'] ? '<p class="wpide-login-error" role="alert">Wrong username or password. Please try again.</p>' : '';
    $html .= wp_login_form( array( 'echo' => false, 'redirect' => $redirect ) );
    $html .= '<p><a href="' . esc_url( wp_lostpassword_url( $current ) ) . '">Lost your password?</a></p>';
    $html .= '</div>';

    return $html;
} );
```

## Notes

Optional attribute: [wpide_login_form redirect="/members/"].

## How to use it

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