How to Add a Front-End Login Form in WordPress

Add a login form to any page in WordPress

Sending users to wp-login.php works, but on a membership site or an online shop it breaks the design: the visitor suddenly sees a different page with the WordPress logo. The login form can be shown on any page of the theme instead.

Problem: Users have to leave the site’s design to log in on wp-login.php.

Solution: Output the standard form with wp_login_form() in a template or a shortcode. If you need your own markup, build the form yourself and log the user in with wp_signon().

The quick way: wp_login_form()

wp_login_form() prints the same fields as the login page (the form still posts to wp-login.php), and after a successful login the user is sent to the redirect address:

<?php
if ( is_user_logged_in() ) {
    printf(
        '<p>%s <a href="%s">%s</a></p>',
        esc_html( sprintf( __( 'Welcome, %s.', 'textdomain' ), wp_get_current_user()->display_name ) ),
        esc_url( wp_logout_url( get_permalink() ) ),
        esc_html__( 'Log out', 'textdomain' )
    );
} else {
    wp_login_form( array(
        'redirect'       => home_url( '/account/' ),
        'remember'       => true,
        'value_remember' => true,
    ) );
}

To use it in the content of a page, wrap it in a shortcode and pass 'echo' => false, so the form is returned instead of printed:

<?php
add_shortcode( 'ha_login_form', function () {
    if ( is_user_logged_in() ) {
        return '';
    }

    return wp_login_form( array( 'echo' => false, 'redirect' => home_url( '/account/' ) ) );
} );

A wrong password still leads to wp-login.php with the error message. If that is a problem, use the second way.

Your own form with wp_signon()

The form posts to the same page, and the handler runs early, before any output, so it can redirect:

<form method="post">
    <?php wp_nonce_field( 'ha_login', 'ha_login_nonce' ); ?>
    <input type="text" name="log" autocomplete="username" required>
    <input type="password" name="pwd" autocomplete="current-password" required>
    <label><input type="checkbox" name="rememberme" value="1"> Remember me</label>
    <button type="submit">Log in</button>
</form>
<?php
add_action( 'template_redirect', 'ha_handle_login_form' );

function ha_handle_login_form() {
    if ( empty( $_POST['ha_login_nonce'] ) || ! wp_verify_nonce( $_POST['ha_login_nonce'], 'ha_login' ) ) {
        return;
    }

    $user = wp_signon( array(
        'user_login'    => sanitize_user( wp_unslash( $_POST['log'] ) ),
        'user_password' => wp_unslash( $_POST['pwd'] ), // never sanitize or log a password
        'remember'      => ! empty( $_POST['rememberme'] ),
    ), is_ssl() );

    if ( is_wp_error( $user ) ) {
        // keep the error for the template, e.g. in a global or a query var
        $GLOBALS['ha_login_error'] = __( 'Wrong username or password.', 'textdomain' );
        return;
    }

    wp_safe_redirect( home_url( '/account/' ) );
    exit;
}

WordPress adds slashes to $_POST, so wp_unslash() is required: without it a password with a quote in it never matches.

NOTE: Check the nonce before wp_signon(), use wp_safe_redirect() for the redirect after login, and show one generic message for a wrong username and a wrong password, so the form does not tell bots which usernames exist.