Halloween Sale 15% off Ends in See the plans
PHP

Front-End Registration Form Shortcode

Adds a [wpide_register] shortcode that shows a sign-up form on any page, styled by your theme, using WordPress's normal registration process and emails.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_register_errors = null;

add_action( 'template_redirect', function () use ( &$wpide_register_errors ) {
    if ( empty( $_POST['wpide_register_nonce'] ) || is_user_logged_in() ) {
        return;
    }

    if ( ! wp_verify_nonce( sanitize_key( $_POST['wpide_register_nonce'] ), 'wpide_register' ) ) {
        $wpide_register_errors = new WP_Error( 'nonce', 'The form expired. Please try again.' );
        return;
    }

    if ( ! get_option( 'users_can_register' ) ) {
        $wpide_register_errors = new WP_Error( 'closed', 'Registration is currently closed.' );
        return;
    }

    $login = isset( $_POST['wpide_user_login'] ) ? sanitize_user( wp_unslash( $_POST['wpide_user_login'] ) ) : '';
    $email = isset( $_POST['wpide_user_email'] ) ? sanitize_email( wp_unslash( $_POST['wpide_user_email'] ) ) : '';
    $result = register_new_user( $login, $email );

    if ( is_wp_error( $result ) ) {
        $wpide_register_errors = $result;
        return;
    }

    wp_safe_redirect( add_query_arg( 'registered', 1, remove_query_arg( 'registered' ) ) );
    exit;
} );

add_shortcode( 'wpide_register', function () use ( &$wpide_register_errors ) {
    if ( is_user_logged_in() ) {
        return '<p class="wpide-register-note">You already have an account.</p>';
    }

    if ( ! get_option( 'users_can_register' ) ) {
        return '<p class="wpide-register-note">Registration is currently closed.</p>';
    }

    if ( isset( $_GET['registered'] ) ) {
        return '<p class="wpide-register-success" role="status">Thanks for signing up! Check your email for a link to set your password.</p>';
    }

    $login = isset( $_POST['wpide_user_login'] ) ? sanitize_user( wp_unslash( $_POST['wpide_user_login'] ) ) : '';
    $email = isset( $_POST['wpide_user_email'] ) ? sanitize_email( wp_unslash( $_POST['wpide_user_email'] ) ) : '';

    ob_start();

    if ( is_wp_error( $wpide_register_errors ) ) {
        echo '<div class="wpide-register-errors" role="alert">';
        foreach ( $wpide_register_errors->get_error_messages() as $message ) {
            echo '<p>' . wp_kses( $message, array( 'strong' => array() ) ) . '</p>';
        }
        echo '</div>';
    }
    ?>
    <form class="wpide-register-form" method="post">
        <p><label for="wpide-user-login">Username</label><br>
        <input type="text" id="wpide-user-login" name="wpide_user_login" value="<?php echo esc_attr( $login ); ?>" autocomplete="username" required></p>
        <p><label for="wpide-user-email">Email</label><br>
        <input type="email" id="wpide-user-email" name="wpide_user_email" value="<?php echo esc_attr( $email ); ?>" autocomplete="email" required></p>
        <?php do_action( 'register_form' ); ?>
        <?php wp_nonce_field( 'wpide_register', 'wpide_register_nonce' ); ?>
        <p><button type="submit">Create account</button></p>
    </form>
    <?php
    return ob_get_clean();
} );

Notes

"Anyone can register" must be enabled in Settings > General. Other registration snippets (invite codes, account types, verification) also apply because it uses the core registration function.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.