Front-end Register Form

in case you want to handle this yourself, here’s what I use:

add_action('template_redirect', 'register_a_user');
function register_a_user(){
  if(isset($_GET['do']) && $_GET['do'] == 'register'):
    $errors = array();
    if(empty($_POST['user']) || empty($_POST['email'])) $errors[] = 'provide a user and email';
    if(!empty($_POST['spam'])) $errors[] = 'gtfo spammer';

    $user_login = esc_attr($_POST['user']);
    $user_email = esc_attr($_POST['email']);
    require_once(ABSPATH.WPINC.'/registration.php');

    $sanitized_user_login = sanitize_user($user_login);
    $user_email = apply_filters('user_registration_email', $user_email);

    if(!is_email($user_email)) $errors[] = 'invalid e-mail';
    elseif(email_exists($user_email)) $errors[] = 'this email is already registered, bla bla...';

    if(empty($sanitized_user_login) || !validate_username($user_login)) $errors[] = 'invalid user name';
    elseif(username_exists($sanitized_user_login)) $errors[] = 'user name already exists';

    if(empty($errors)):
      $user_pass = wp_generate_password();
      $user_id = wp_create_user($sanitized_user_login, $user_pass, $user_email);

      if(!$user_id):
        $errors[] = 'registration failed...';
      else:
        update_user_option($user_id, 'default_password_nag', true, true);
        wp_new_user_notification($user_id, $user_pass);
      endif;
    endif;

    if(!empty($errors)) define('REGISTRATION_ERROR', serialize($errors));
    else define('REGISTERED_A_USER', $user_email);
  endif;
}

the code is almost identical to the one from the user signup page.

then add your form in your template:

<?php
  if(defined('REGISTRATION_ERROR'))
    foreach(unserialize(REGISTRATION_ERROR) as $error)
      echo "<div class=\"error\">{$error}</div>";
  // errors here, if any

  elseif(defined('REGISTERED_A_USER'))
    echo 'a email has been sent to '.REGISTERED_A_USER;
?>
<form method="post" action="<?php echo add_query_arg('do', 'register', home_url('/')); ?>">
  <label>
    User:
    <input type="text" name="user" value=""/>
  </label>

  <label>
    Email:
   <input type="text" name="email" value="" />
  </label>

  <label>
    Delete this text:
   <input type="text" name="spam" value="some_crappy_spam_protection" />
  </label>

  <input type="submit" value="register" />
</form>

you can either create a widget with this, a shortcode or just the usual page template…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s