1

I have a front end registration form which almost works perfect except that the passwords are not being set correctly.

In my functions.php I have -

/**
 * Registration form
 *
 */

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';
    if(!empty($_POST['pass1']) && !empty($_POST['pass2'])) $error[] = 'The passwords you entered do not match';

$account = esc_attr($_POST['account_type']);
    $user_login = esc_attr($_POST['user']);
    $user_email = esc_attr($_POST['email']);
 $user_pass = esc_attr($_POST['pass1']);
 $user_pass2 = esc_attr($_POST['pass2']);
    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)):
 if ( $_POST['pass1'] == $_POST['pass2'] ) {

 $user_data = array (
        'user_login' => $sanitized_user_login,
        'user_password' => $user_pass,     
        'user_email' => $user_email,
        'role' => $account
    );

    // Create the user
    $user_id = wp_insert_user( $user_data );

} else { 
$errors[] = 'passwords dont match'; 
}


      if(!$user_id):
        $errors[] = 'registration failed...';
      else:
        wp_new_user_notification($user_id);
      endif;
    endif;

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

And on my register page 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;
?>

<h2>REGISTER</h2>
<!-- REGISTER FORM -->
<form method="post" id="register-form" action="<?php echo add_query_arg('do', 'register'); ?>">

<!-- CHOOSE ACCOUNT TYPE AKA USER ROLE -->
<h4>Choose Account Type</h4>
<select name="account_type" class="form-control">
<option value="agent">Agent</option>
<option value="broker">Broker</option>
</select>

<!-- ENTER ACCOUNT DETAILS -->
<h4>Account Details</h4>

<input type="text" name="email" class="form-control" value="" placeholder="Email" />

<input type="text" name="user" class="form-control" value="" placeholder="Username"/>

<input type="text" name="pass1" class="form-control" value="" placeholder="Password" />

<input type="text" name="pass2" class="form-control" value="" placeholder="Confirm Password" />

Delete this text
<input type="text" name="spam" class="form-control" value="spam_protection" />

<!-- REGISTER BUTTON -->
<input type="submit" value="REGISTER" />

</form>
  • 1
    Your $user_data has incorrect array key for password. It needs to be user_pass instead of user_password. I'll post this as answer if this works for you – Piyush Rawat Nov 7 '17 at 6:58
  • wow, I cant believe I didnt realize that, thanks. Go ahead and post as answer. I'm sure others could find a use for the code. – Rich Nov 7 '17 at 7:25
3

$user_data has incorrect array key for password. It needs to be user_pass instead of user_password. For more reference please check out wp_insert_user()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.